#include "bits/stdc++.h" #include "atcoder/all" using namespace std; using namespace atcoder; //using mint = modint1000000007; //const int mod = 1000000007; using mint = modint998244353; const int mod = 998244353; //const int INF = 1e9; //const long long LINF = 1e18; //const bool debug = false; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i,l,r)for(int i=(l);i<(r);++i) #define rrep(i, n) for (int i = (n-1); i >= 0; --i) #define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i) #define all(x) (x).begin(),(x).end() #define allR(x) (x).rbegin(),(x).rend() #define endl "\n" #define P pair template inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } string s; int mex(int x, int y) { int z; if ((0 == x) && (0 == y))z = 1; if ((0 == x) && (1 == y))z = 2; if ((0 == x) && (2 == y))z = 1; if ((1 == x) && (0 == y))z = 2; if ((1 == x) && (1 == y))z = 0; if ((1 == x) && (2 == y))z = 0; if ((2 == x) && (0 == y))z = 1; if ((2 == x) && (1 == y))z = 0; if ((2 == x) && (2 == y))z = 0; return z; } vector dfs(int l, int r) { //cout << l << " " << r << endl; //cout << s.substr(l, r - l) << endl;; vectorv(3); if ('m' == s[l]) { int pos = 0; int count = 0; rep2(i, l, r) { if (',' == s[i]) { if (1 == count) { pos = i; break; } } else if ('(' == s[i]) { count++; } else if (')' == s[i]) { count--; } } auto lv = dfs(l + 4, pos); auto rv = dfs(pos + 1, r - 1); if ('a' != s[l + 1]) { //mex rep(i, 3)rep(j, 3) v[mex(i, j)] += lv[i] * rv[j]; } if ('e' != s[l + 1]) { //max rep(i, 3)rep(j, 3) v[max(i, j)] += lv[i] * rv[j]; } } else { if ('?' == s[l]) { rep(i, 3)v[i]++; } else { v[s[l] - '0']++; } } return v; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> s; int k; cin >> k; auto v = dfs(0, s.size()); cout << v[k].val() << endl; return 0; }