#include #include #include #include #include #include #include #include #include using namespace std; void timer(){ static chrono::steady_clock::time_point start = chrono::steady_clock::now(); auto now = chrono::steady_clock::now(); cerr << "elapsed time : " << chrono::duration_cast(now-start).count() << "[ms]" << endl; } const long long mod = 1000000007; long long ans[10001]; long long fact[10001]; long long fact_inv[10001]; // long long nCr_[10001][10001]; long long mod_pow(long long x, long long y, long long mod){ //(x^y) % mod if(x==0 && y!=0) return 0; long long ret=1LL; while(y>0LL){ if(y&1LL) ret = (ret * x) % mod; x = (x*x) % mod; y >>= 1LL; } return ret; } long long nCr(long long n, long long r){ // if(nCr_[n][r]) return nCr_[n][r]; return fact[n] * fact_inv[r] % mod * fact_inv[n-r] % mod; } struct trie_node{ map child; int count; }; vector trie; vector s[200001]; vector w[200001]; // dp[i][x] := i 頂点で合計長 x の組み合わせ void dfs(int pos, long long d){ if( trie[pos].count == 1 ){ w[pos].resize(2); s[pos].resize(2); w[pos][0] = w[pos][1] = 1; s[pos][0] = 0; s[pos][1] = 1; return; } if( trie[pos].child.size() == 1 ){ int nx = trie[pos].child.begin()->second; dfs(nx, d+1); swap( s[pos], s[nx] ); swap( w[pos], w[nx] ); return; } w[pos].resize(1); s[pos].resize(1); w[pos][0] = 1; s[pos][0] = 0; for(auto p : trie[pos].child){ if( p.first == '}' ) continue; vector w_, s_; int nx = p.second; dfs(nx, 1); for(int i=0; i= w_.size()){ w_.resize(i+j+1); s_.resize(i+j+1); } (w_[i+j] += w[pos][i] * w[nx][j] % mod * nCr(i+j, i) % mod) %= mod; (s_[i+j] += s[pos][i] * w[nx][j] % mod * nCr(i+j, i) % mod) %= mod; (s_[i+j] += w[pos][i] * s[nx][j] % mod * nCr(i+j, i) % mod) %= mod; } } swap(w[pos], w_); swap(s[pos], s_); } w[pos].resize( trie[pos].count + 1 ); s[pos].resize( trie[pos].count + 1 ); for(long long i=1; i(i, trie[pos].count-1)%mod * w[pos][i]) %= mod; } } int main(){ timer(); fact[0] = fact_inv[0] = 1; for(int i=1; i<10001; i++){ fact[i] = fact[i-1] * i % mod; fact_inv[i] = mod_pow(fact[i],mod-2,mod); } int n; cin >> n; vector s_(n); for(int i=0; i> s_[i]; s_[i] += '{'; } s_.push_back("}"); trie.push_back( trie_node{{}, 0} ); for(int i=0; i