#include #include using namespace std; const int MOD = 666013; int sz; int groups[30]; bool frecv[30]; int special_group; int dp[1 << 17][30]; bool vis[1 << 17][30]; bool blocked[30][30]; int compute(int state, int last) { if (!state) { if (!special_group) return !blocked[last][last]; else return !blocked[last][special_group]; } if (vis[state][last]) return dp[state][last]; vis[state][last] = true; for (int i = 1; i <= 26; ++ i) if (!blocked[last][i]) { int new_state = state; int j; for (j = 0; j < sz; ++ j) if (state & (1 << j)) if (groups[j] == i) { new_state ^= (1 << j); break; } if (j < sz) { dp[state][last] += compute(new_state, i); if (dp[state][last] >= MOD) dp[state][last] -= MOD; } } return dp[state][last]; } int main() { string str; cin >> str; int grrr = 0; for (auto it: str) if (!frecv[it - 'a' + 1]) { frecv[it - 'a' + 1] = 1; ++ grrr; } else { frecv[it - 'a' + 1] = 0; groups[sz ++] = it - 'a' + 1; -- grrr; } if (grrr > 1) { cout << "0\n"; return 0; } else if (grrr == 1) { for (int i = 1; i <= 26; ++ i) if (frecv[i]) special_group = i; } int m = 0; cin >> m; char x, y; while (m --) { cin >> x >> y; x -= 'a'; y -= 'a'; ++ x; ++ y; blocked[x][y] = blocked[y][x] = true; } cout << compute((1 << sz) - 1, 0) << '\n'; return 0; }