#ifndef LOCAL #pragma GCC optimize("O3") #endif #include using namespace std; #define sim template muu & operator<<( #define ris return *this #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 i, c j) {return rge{i, j};} 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 mor rge u) { a << "["; for (c i = u.b; i != u.e; ++i) *this << ", " + 2 * (i == u.b) << *i; ris << "]"; } sim, class b mor pair r){ris << "(" << r.first << ", " << r.second << ")";} #else sim mor const c&){ris;} #endif }; #define debug muu() << __FUNCTION__ << "#" << __LINE__ << ": " #define imie(r...) "[" #r ": " << (r) << "] " #define range(a, b) "[[" #a ", " #b "): " << range(a, b) << "] " #define arr(a, i) "[" #a imie(i) ": " << a[i] << "] " using ll = long long; using ld = long double; using pii = pair ; const int MAXN= 404, sigma = 26; int term[MAXN], len[MAXN], to[MAXN][sigma], link[MAXN], sz = 1; void add_str(string s) { int cur = 0; for(auto c : s) { if(!to[cur][c - 'a']) { to[cur][c - 'a'] = sz++; len[to[cur][c - 'a']] = len[cur] + 1; } cur = to[cur][c - 'a']; } term[cur] = cur; } void push_links() { int que[sz]; int st = 0, fi = 1; que[0] = 0; while(st < fi) { int V = que[st++]; int U = link[V]; if(!term[V]) term[V] = term[U]; for(int c = 0; c < sigma; ++c) { if(to[V][c]) { link[to[V][c]] = V ? to[U][c] : 0; que[fi++] = to[V][c]; } else { to[V][c] = to[U][c]; } } } } vector> make_mat() { return vector>(sz, vector(sz, 0)); } const ll mod = 1e9 + 7; vector> mnoz(const vector>& A, const vector>& B) { auto res = make_mat(); int n = res.size(); for(int i = 0; i < n; ++i) for(int j = 0; j < n; ++j) { ll val = 0; for(int k = 0; k < n; ++k) { val += 1ll * A[i][k] * B[k][j]; if(val > 5 * mod) val %= mod; } res[i][j] = val % mod; } return res; } char buff[1000]; int N, Q; int main() { scanf("%d%d", &N, &Q); for(int i = 0; i < Q; ++i) { int ln; scanf("%d", &ln); scanf("%s", buff); string str(buff); add_str(str); } debug << "kurwa"; push_links(); debug << "kurwa2"; auto mat = make_mat(); for(int stan = 0; stan < sz; ++stan) for(int s = 0; s < sigma; ++s) mat[stan][to[stan][s]]++; for(int stan = 0; stan < sz; ++stan) if(term[stan] > 0) for(int s2 = 0; s2 < sz; ++s2) mat[s2][stan] = 0; auto res = make_mat(); for(int i = 0; i < sz; ++i) res[i][i] = 1; int exp = N; while(exp > 0) { if(exp % 2 == 1) res = mnoz(res, mat); exp /= 2; mat = mnoz(mat, mat); debug << imie("chuj"); } ll tot_res = 0; for(int i = 0; i < sz; ++i) tot_res += res[0][i]; tot_res %= mod; printf("%lld\n", tot_res); }