#include const int Mod = 998244353; int main() { char S[200001]; int K; scanf("%s", S); scanf("%d", &K); if (S[1] == 0) { if (S[0] == '?') printf("3\n"); else if (S[0] - '0' == K) printf("1\n"); else printf("0\n"); fflush(stdout); return 0; } int i, j, k, par[200001], s[200001], head = 1; s[0] = -1; for (i = 0; S[i] != 0; i++) { if (S[i] == ',' || S[i] == ')') par[i] = -2; else { par[i] = s[--head]; if (S[i] == 'm') { s[head++] = i; s[head++] = i; par[i+1] = -2; par[i+2] = -2; par[i+3] = -2; i += 3; } } } int child[200001][2] = {}; long long dp[200001][3] = {}; for (i = 0; S[i] != 0; i++) { if (par[i] < 0) continue; else if (child[par[i]][1] == 0) child[par[i]][1] = i; else child[par[i]][0] = i; } for (i--; i >= 0; i--) { if (par[i] == -2) continue; if (child[i][0] == 0) { if (S[i] == '?') { dp[i][0] = 1; dp[i][1] = 1; dp[i][2] = 1; } else dp[i][S[i] - '0'] = 1; } else { j = child[i][0]; k = child[i][1]; if (S[i+1] == '?') { dp[i][0] = (dp[j][0] * dp[k][0] + (dp[j][1] + dp[j][2]) * (dp[k][1] + dp[k][2])) % Mod; dp[i][1] = (dp[j][1] * (dp[k][0] + dp[k][1]) + dp[k][1] * dp[j][0] + dp[j][0] * (dp[k][0] + dp[k][2]) + dp[k][0] * dp[j][2]) % Mod; dp[i][2] = (dp[j][2] * (dp[k][0] + dp[k][1] + dp[k][2]) + dp[k][2] * (dp[j][0] + dp[j][1]) + dp[j][0] * dp[k][1] + dp[k][0] * dp[j][1]) % Mod; } else if (S[i+1] == 'a') { dp[i][0] = dp[j][0] * dp[k][0] % Mod; dp[i][1] = (dp[j][1] * (dp[k][0] + dp[k][1]) + dp[k][1] * dp[j][0]) % Mod; dp[i][2] = (dp[j][2] * (dp[k][0] + dp[k][1] + dp[k][2]) + dp[k][2] * (dp[j][0] + dp[j][1])) % Mod; } else { dp[i][0] = (dp[j][1] + dp[j][2]) * (dp[k][1] + dp[k][2]) % Mod; dp[i][1] = (dp[j][0] * (dp[k][0] + dp[k][2]) + dp[k][0] * dp[j][2]) % Mod; dp[i][2] = (dp[j][0] * dp[k][1] + dp[k][0] * dp[j][1]) % Mod; } } } printf("%lld\n", dp[0][K]); fflush(stdout); return 0; }