#include using namespace std; using ll = long long; using Vi = vector; using Pii = pair; #define mp make_pair #define pb push_back #define x first #define y second #define rep(i,b,e) for(int i = (b); i < (e); i++) #define each(a, x) for(auto& a : (x)) #define all(x) (x).begin(),(x).end() #define sz(x) int((x).size()) #define tem template #define pri(x,y)tem auto operator<<(t& o, u a)->decltype(x,o) { o << y; return o; } pri(a.print(), "{"; a.print(); "}"); pri(a.y, "(" << a.x << ", " << a.y << ")"); pri(all(a), "["; auto d=""; for (auto i : a) (o << d << i, d = ", "); o << "]"); void DD(...) {} tem void DD(t s, u a, w... k) { for (int b = 44; *s && *s - b; cerr << *s++) b += ((*s == 41) - (*s == 40)) * 88; cerr << ": " << a << *s++; DD(s, k...); } tem vector span(const t* a, u n) { return {a, a+n}; } #ifdef LOC #define deb(...) (DD("[,\b :] "#__VA_ARGS__, __LINE__, __VA_ARGS__), cerr << endl) #else #define deb(...) #endif #define DBP(...) void print() { DD(#__VA_ARGS__, __VA_ARGS__); } struct Aho { enum{alpha = 26, first = 'a'}; struct Node { int back, next[alpha], start = -1, end = -1, nmatches = 0; Node(int v) { memset(next, v, sizeof(next)); } }; vector N; vector backp; void insert(string& s, int j) { assert(!s.empty()); int n = 0; each(c, s) { int& m = N[n].next[c-first]; if (m == -1) { n = m = sz(N); N.emplace_back(-1); } else { n = m; } } if (N[n].end == -1) N[n].start = j; backp.push_back(N[n].end); N[n].end = j; N[n].nmatches++; } Aho(vector& pat) { N.emplace_back(-1); rep(i, 0, sz(pat)) insert(pat[i], i); N[0].back = sz(N); N.emplace_back(0); queue q; for (q.push(0); !q.empty(); q.pop()) { int n = q.front(), prev = N[n].back; rep(i, 0, alpha) { int &ed = N[n].next[i], y = N[prev].next[i]; if (ed == -1) ed = y; else { N[ed].back = y; (N[ed].end == -1 ? N[ed].end : backp[N[ed].start]) = N[y].end; N[ed].nmatches += N[y].nmatches; q.push(ed); } } } } }; constexpr ll MOD = 1000000007; using Mat = vector>; Mat mul(Mat a, Mat b) { int n = sz(a); Mat c(n, vector(n)); rep(i, 0, n) { rep(j, 0, n) { rep(k, 0, n) { c[i][j] += a[i][k] * b[k][j] % MOD; c[i][j] %= MOD; } } } return c; } Mat matPow(Mat a, ll e) { int n = sz(a); Mat t(n, vector(n)); rep(i, 0, n) { t[i][i] = 1; } while (e > 0) { if (e % 2) { t = mul(t, a); } e /= 2; a = mul(a, a); } return t; } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(12); ll n; int q; cin >> n >> q; vector elems(q); each(e, elems) { int l; cin >> l; cin >> e; //assert(sz(e) == l); } Aho aho(elems); int kek = sz(aho.N); Mat mat(kek, vector(kek)); rep(i, 0, kek) { rep(a, 0, 26) { int j = aho.N[i].next[a]; if (aho.N[j].nmatches == 0) { mat[i][j] += 1; mat[i][j] %= MOD; } } } Mat res = matPow(mat, n); ll ans = 0; //deb(mat, res); rep(i, 0, kek) { ans += res[0][i]; ans %= MOD; } deb(MOD); ans %= MOD; if (ans < 0) ans += MOD; cout << ans << endl; return 0; }