#ifndef LOCAL #pragma GCC optimize("O3") #endif #include using namespace std; #define sim template < class c #define ris return * this #define mor > muu & operator << ( #define R22(r) sim > typename \ enable_if<1 r sizeof dud(0), muu&>::type operator<<( c g) { sim > struct rge { c b, e; }; sim > rge range(c h, c n) { return {h, n}; } sim > auto dud(c * r) -> decltype(cerr << *r); sim > char dud(...); struct muu { #ifdef LOCAL stringstream a; ~muu() { cerr << a.str() << endl; } R22(<) a << boolalpha << g; ris; } R22(==) ris << range(begin(g), end(g)); } sim, class b mor pair < b, c> r) { ris << "(" << r.first << ", " << r.second << ")"; } sim mor rge u) { a << "["; for (c i = u.b; i != u.e; ++i) *this << ", " + 2 * (i == u.b) << *i; ris << "]"; } #else sim mor const c&) { ris; } #endif muu & operator()() { ris; } }; #define imie(r...) "[" #r ": " << (r) << "] " #define debug (muu() << __FUNCTION__ << "#" << __LINE__ << ": ") int licz = 0; class aho { public: aho* suflink; int id; int koniec; map kra; aho(vector& all) { suflink = nullptr; id = licz ++; koniec = 0; all.push_back(this); } aho* daj_kra(char x, vector& all) { aho* r = kra[x]; if (r == nullptr) { kra[x] = new aho(all); r = kra[x]; } return r; } int idz(char x) { if (kra[x] != nullptr) return kra[x] -> id; aho* link = suflink; while (link -> kra[x] == nullptr) link = link -> suflink; return link -> kra[x] -> id; } }; vector all; void wstaw(string slo, aho* node) { for (char x : slo) { node = node -> daj_kra(x, all); } node -> koniec ++; } void ahobfs(aho* eps, aho* root) { root -> suflink = eps; queue kol; kol.push(root); while (!kol.empty()) { aho* node = kol.front(); kol.pop(); for (auto p : node -> kra) { char x = p.first; aho* y = p.second; if (y == nullptr) continue; aho* link = node -> suflink; while (link -> kra[x] == nullptr) link = link -> suflink; y -> suflink = link -> kra[x]; kol.push(y); } } } vector> macierz; void ahomacierz() { int n = all.size(); macierz.resize(n); for (int i = 0; i < n; i ++) { macierz[i].resize('z' - 'a' + 1); for (int j = 'a'; j <= 'z'; j ++) { macierz[i][j - 'a'] = all[i] -> idz(j); } } } const int N = 107; const int mod = 1e9 + 7; long long pom[N][N]; int A[N][N]; int ans[N][N]; void mul(int (&a)[N][N], int (&b)[N][N], int (&c)[N][N], int n) { for (int i = 0; i < n; ++i) { for (int k = 0; k < n; ++k) { for (int j = 0; j < n; ++j) { pom[i][j] += ((long long) a[i][k] * b[k][j]) % mod; } } } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { c[i][j] = pom[i][j] % mod; pom[i][j] = 0; } } } void solve() { int n, q; cin >> n >> q; aho* eps = new aho(all); aho* root = new aho(all); for (int i = 'a'; i <= 'z'; i ++) eps -> kra[i] = root; for (int i = 0; i < q; i ++) { int k; string s; cin >> k >> s; wstaw(s, root); } ahobfs(eps, root); ahomacierz(); int st = all.size(); for (int i = 0; i < st; ++i) { for (int j = 0; j < 26; ++j) A[i][macierz[i][j]]++; } for (int i = 0; i < st; ++i) { if (all[i] -> koniec) { for (int j = 0; j < st; ++j) A[i][j] = A[j][i] = 0; } } //~ for (int i = 0; i < st; ++i) { //~ for (int j = 0; j < st; ++j) { //~ cerr << A[i][j] << ' '; //~ } //~ cerr << endl; //~ } for (int i = 0; i < st; ++i) ans[i][i] = 1; while (n > 0) { if (n & 1) { mul(ans, A, ans, st); } mul(A, A, A, st); n /= 2; } //~ for (int i = 0; i < st; ++i) { //~ for (int j = 0; j < st; ++j) { //~ cerr << ans[i][j] << ' '; //~ } //~ cerr << endl; //~ } long long res = 0; for (int i = 0; i < st; ++i) { debug << imie(i) imie(all[i] -> koniec); res += ans[1][i]; } res %= mod; cout << res << endl; debug << macierz; } int main() { ios_base::sync_with_stdio(0); solve(); }