#include using namespace std; const long long MOD = 1e9+7; long long inv(long long x){ x%=MOD; assert(x); long long ret=1ll; for(long long i=1;i<=MOD;i<<=1){ if((MOD-2)&i){ ret = ret*x%MOD; } x = x*x%MOD; } return ret; } vector fcache; vector ifcache; long long fact(int i){ return fcache[i]; } long long factinv(int i){ return ifcache[i]; } void buildf(int top){ top+=3; fcache.resize(top); ifcache.resize(top); for(int i=fcache[0]=1;i0;--i)ifcache[i-1]=i*ifcache[i]%MOD; assert(ifcache[0]==1); } void gen(int N){ freopen("out.txt", "w", stdout); cout << N << "\n"; for(int i=1;i<=N;++i){ cout << i << " "; } cout << endl; exit(0); } //O(N^3/6), could be O(N^2 log N) by using FFT. signed main(){ #ifdef LOCAL_RUN freopen("in.txt", "r", stdin); #endif // LOCAL_RUN int N;cin >> N; if(N<0) gen(-N); buildf(N); vector a(N); copy_n(istream_iterator(cin), N, a.begin()); vector > DP(N+1, vector(N+1, 0)); fill(DP[0].begin(), DP[0].end(), 1ll); for(int i=1;i<=N;++i){ for(int j=i;j>=0;--j){ if(ja[j+1]) break; DP[i][i-j]=fact(i-j)*DP[j][i-j]%MOD; } for(int j=0;j<=i;++j){ //cerr << i << "/" << j << " : " << DP[i][j] << "\n"; for(int k=j+1;k<=i;++k){/// FFT ?? DP[i][j]+=DP[i][k]*factinv(k-j)%MOD; } DP[i][j]%=MOD; } //for(int j=N;j>=0;--j) cerr << i << "/" << j << " : " << DP[i][j] << "\n"; } cout << DP[N][0] << "\n"; }