#include #include #include using namespace std; using ll = long long; using P = pair>; constexpr ll MOD = 998244353; vector

G; vector> dp; int expr(const string &s, int &i){ // cerr << i << " " << s[i] << ", "; int ret = G.size(); if(s[i] == 'm'){ G.emplace_back(0, vector{}); if(s[i+1] == 'a'){// max G[ret].first = 4; }else if(s[i+1] == 'e'){// mex G[ret].first = 5; }else{// m?x G[ret].first = 6; } i += 4; int x = expr(s, i); G[ret].second.emplace_back(x); // cerr << "add ok" << endl; i++; int y = expr(s, i); G[ret].second.emplace_back(y); i++; }else{ G.emplace_back(s[i] == '?' ? 3 : s[i]-'0', vector{}); i++; } // cerr << "ret: " << ret << ", "; return ret; } ll f(int v, int x){ if(dp[x][v] != -1) return dp[x][v]; auto mex = [](int a, int b){ for(int res = 0; ; res++){ if(res != a && res != b) return res; } }; dp[x][v] = 0; if(G[v].first < 3) dp[x][v] = x == G[v].first ? 1 : 0; else if(G[v].first == 3) dp[x][v] = 1; else{ if(G[v].first == 4 || G[v].first == 6){ for(int i = 0; i < 3; i++){ for(int j = 0; j < 3; j++){ if(max(i, j) == x){ dp[x][v] += f(G[v].second[0], i)*f(G[v].second[1], j)%MOD; dp[x][v] %= MOD; } } } } if(G[v].first == 5 || G[v].first == 6){ for(int i = 0; i < 3; i++){ for(int j = 0; j < 3; j++){ if(mex(i, j) == x){ dp[x][v] += f(G[v].second[0], i)*f(G[v].second[1], j)%MOD; dp[x][v] %= MOD; } } } } } return dp[x][v]; } int main(){ string s; cin >> s; int k; cin >> k; { int i = 0; expr(s, i); } dp = vector>(3, vector(G.size(), -1)); cout << f(0, k) << endl; return 0; }