cin >> n >> s + 1 >> m; for (int i = 1; i <= n; ++i) { a[i][i] = make_pair(0, 0); cnt += s[i] == '?'; } for (int len = 2; len <= n; ++len) for (int i = 1, j = len; j <= n; ++i, ++j) { a[i][j] = a[i + 1][j - 1]; if (a[i][j].first == -1) continue; if (s[i] == '?' && s[j] == '?') ++a[i][j].second; elseif (s[i] == '?') a[i][j].first |= 1 << s[j] - 'a', ++a[i][j].second; elseif (s[j] == '?') a[i][j].first |= 1 << s[i] - 'a', ++a[i][j].second; elseif (s[i] != s[j]) a[i][j] = make_pair(-1, -1); } int N = 17, M = (1 << N) - 1; for (int i = 0; i <= n; ++i) ppow[0][i] = 1; for (int o = 1; o <= N; ++o) { ppow[o][0] = 1; for (int i = 1; i <= n; ++i) ppow[o][i] = ppow[o][i - 1] * o % p; } for (int i = 1; i <= N; ++i) { for (int S = 0; S <= M; ++S) f[S] = 0; for (int l = 1; l <= n; ++l) for (int r = l; r <= n; ++r) if (a[l][r].first != -1) f[a[l][r].first] = add(f[a[l][r].first], ppow[i][cnt - a[l][r].second]); for (int o = 0; o < N; ++o) for (int S = 0; S <= M; ++S) if (S >> o & 1) f[S] = add(f[S], f[S ^ 1 << o]); for (int S = 0; S <= M; ++S) if (__builtin_popcount(S) == i) ans[S] = add(ans[S], f[S]); } for (int i = 1; i <= m; ++i) { char s[30]; int len, S = 0; cin >> s; len = strlen(s); for (int j = 0; j < len; ++j) S |= 1 << s[j] - 'a'; cout << ans[S] << "\n"; } return0; }