#include using namespace std; typedef long long ll; typedef pair pii; const ll kMod = 1000000007; vector> times(const vector> &a, const vector> &b) { vector> result(a.size(), vector(b[0].size(), 0)); for (int y = 0; y < a.size(); y++) { for (int x = 0; x < b[y].size(); x++) { for (int j = 0; j < b.size(); j++) { result[y][x] += a[y][j] * b[j][x]; result[y][x] %= kMod; } } } return result; } vector> power(const vector> &a, ll exp) { vector> result(a.size(), vector(a.size(), 0)); for (unsigned i = 0; i < a.size(); i++) result[i][i] = 1; vector> base = a; while (exp > 0) { if (exp & 1) { result = times(result, base); } base = times(base, base); exp = (exp >> 1); } return result; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n, q; cin >> n >> q; vector phrases(q); for (int i = 0; i < q; i++) { int l; cin >> l >> phrases[i]; } map state_to_index; vector index_to_state; for (int i = 0; i < q; i++) { for (int j = 0; j <= phrases[i].size(); j++) { string state = phrases[i].substr(0, j); if (state_to_index.count(state) == 0) { state_to_index[state] = index_to_state.size(); index_to_state.push_back(state); } } } int total = index_to_state.size(); vector> matrix(total, vector(total, 0)); vector forbidden(total, false); for (string phrase : phrases) { forbidden[state_to_index[phrase]] = true; } for (int index = 0; index < total; index++) { string state = index_to_state[index]; for (char c = 'a'; c <= 'z'; c++) { state.push_back(c); int longest = -1; int n_ind = -1; bool bad = false; for (int next_index = 0; next_index < total; next_index++) { string next_state = index_to_state[next_index]; if (state.size() >= next_state.size()) { if (state.substr(state.size() - next_state.size()) == next_state) { if (forbidden[next_index]) { bad = true; break; } if (static_cast(next_state.size()) > longest) { longest = next_state.size(); n_ind = next_index; } } } } if (!bad) matrix[index][n_ind]++; state.pop_back(); } } vector> start(1, vector(total, 0)); start[0][0] = 1; vector> final_matrix = power(matrix, n); vector> result_vec = times(start, final_matrix); ll result = 0; for (int i = 0; i < total; i++) { result += result_vec[0][i]; result %= kMod; } cout << result << endl; return 0; }