#include <bits/stdc++.h>
using namespace std;
int n, m, q[6];
long long x[1005][1 << 6], ans;
char y[5], z[5];
vector<int> w[6][6];
int main() {
  scanf("%d%d", &n, &m);
  for (int a = 0; a < m; a++) {
    scanf("%s%s", y, z);
    w[z[0] - 'a'][y[1] - 'a'].push_back(y[0] - 'a');
  }
  x[0][1] = 1;
  for (int a = 0; a < n - 1; a++)
    for (int b = 1; b < 1 << 6; b++)
      if (x[a][b]) {
        for (int c = 0; c < 6; c++) q[c] = 0;
        for (int c = 0; c < 6; c++)
          if (b & (1 << c))
            for (int d = 0; d < 6; d++)
              for (int e = 0; e < w[c][d].size(); e++) q[d] |= 1 << w[c][d][e];
        for (int c = 0; c < 6; c++)
          if (q[c]) (x[a + 1][q[c]] += x[a][b]) %= 1000000007;
      }
  for (int a = 1; a < 1 << 6; a++)
    if (x[n - 1][a]) {
      m = 0;
      for (int b = 0; b < 6; b++)
        if (a & (1 << b)) m++;
      (ans += x[n - 1][a] * m) %= 1000000007;
    }
  printf("%I64d\n", ans);
  return 0;
}
