#include #include using namespace std; using namespace atcoder; #define rep(i,n)for (int i = 0; i < int(n); ++i) #define rrep(i,n)for (int i = int(n)-1; i >= 0; --i) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() template void chmax(T& a, const T& b) {a = max(a, b);} template void chmin(T& a, const T& b) {a = min(a, b);} using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; using mint = modint998244353; int main() { ios::sync_with_stdio(false); cin.tie(0); string s; cin >> s; const int n = s.size(); VI rparen(n, -1); VI comm(n, -1); VI st; rep(i, n) { if (s[i] == '(') st.push_back(i); else if (s[i] == ')') { rparen[st.back()] = i; st.pop_back(); } else if (s[i] == ',') { comm[st.back()] = i; } } auto mex = [&](int i, int j) { if (i > j) swap(i, j); if (i) return 0; if (j == 1) return 2; else return 1; }; using T = array; auto dfs = [&](auto&& self, int left, int right) { T res; if ('0' <= s[left] && s[left] <= '2') { res[s[left] - '0'] = 1; return res; } if (s[left] == '?') { rep(i, 3) res[i] = 1; return res; } int lp = left + 3; T r1 = self(self, lp + 1, comm[lp]); T r2 = self(self, comm[lp] + 1, rparen[lp]); if (s[left + 1] != 'e') { rep(i, 3) rep(j, 3) res[max(i, j)] += r1[i] * r2[j]; } if (s[left + 1] != 'a') { rep(i, 3) rep(j, 3) res[mex(i, j)] += r1[i] * r2[j]; } return res; }; T res = dfs(dfs, 0, n); int k; cin >> k; cout << res[k].val() << endl; }