#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++) using namespace std; typedef long long int ll; typedef vector VI; typedef vector VL; typedef pair PI; const ll mod = 1e9 + 7; ll powmod(ll a, ll e) { ll sum = 1; ll cur = a; while (e > 0) { if (e % 2) { sum = sum * cur % mod; } cur = cur * cur % mod; e /= 2; } return sum; } const int N = 2437; ll fact[N]; ll comb_tbl[N][N]; void init(void) { fact[0] = 1; REP(i, 1, N) { fact[i] = fact[i - 1] * i % mod; } REP(i, 0, N) { REP(j, 0, i + 1) { comb_tbl[i][j] = fact[i] * powmod(fact[i - j] * fact[j] % mod, mod - 2) % mod; } } } const int DEBUG = 0; vector get_lcp(const vector > &pool) { int n = pool.size(); vector lcp(n, VI(n)); VI neighbor(n - 1); REP(i, 0, n - 1) { const string& u = pool[i].first; const string& v = pool[i + 1].first; int idx = 0; while (true) { if ((int) u.length() <= idx || (int) v.length() <= idx) { break; } if (u[idx] == v[idx]) { idx += 1; } else { break; } } neighbor[i] = idx; } REP(i, 0, n - 1) { int cur = 1e8; REP(j, i + 1, n) { cur = min(cur, neighbor[j - 1]); lcp[pool[i].second][pool[j].second] = cur; lcp[pool[j].second][pool[i].second] = cur; } } REP(i, 0, n) { int idx = pool[i].second; lcp[idx][idx] = pool[i].first.size(); } return lcp; } int main(void) { init(); int n; cin >> n; vector s(n); vector > pool(n); REP(i, 0, n) { cin >> s[i]; s[i] += '$'; pool[i] = make_pair(s[i], i); } sort(pool.begin(), pool.end()); vector lcp = get_lcp(pool); if (DEBUG) { REP(i, 0, n) { cerr << "lcp[" << i << "]:"; REP(j, 0, n) { cerr << " " << lcp[i][j]; } cerr << endl; } } REP(i, 0, n) { VI tap = lcp[i]; sort(tap.begin(), tap.end()); tap.pop_back(); // drop i itself lcp[i] = tap; } vector dp(n + 1); dp[0] = 0; REP(m, 1, n + 1) { if (DEBUG) { cerr << "** m = " << m << endl; } ll tot = (n - m + 1) * dp[m - 1] % mod; ll fac = fact[m - 1]; REP(i, 0, n) { // exhaustive search of S s.t. i \in S, |S| = m VI tap = lcp[i]; if (DEBUG) { cerr << "tap:"; for(auto v:tap)cerr<<" " <