#include #include #include #include using namespace std; using i32 = int32_t; using u32 = uint32_t; using i64 = int64_t; using u64 = uint64_t; #define rep(i,n) for(int i=0; i<(n); i++) using m32 = atcoder::modint998244353; struct Exp{ m32 A[3] = {}; }; int mex(int a, int b){ static const int mat[3][3] = { {1,2,1},{2,0,0},{1,0,0} }; return mat[a][b]; } Exp operator+(Exp a, Exp b){ Exp res; rep(i,3) res.A[i] = a.A[i] + b.A[i]; return res; } Exp maxExp(Exp a, Exp b){ Exp res; rep(i,3) rep(j,3) res.A[max(i,j)] += a.A[i] * b.A[j]; return res; } Exp mexExp(Exp a, Exp b){ Exp res; rep(i,3) rep(j,3) res.A[mex(i,j)] += a.A[i] * b.A[j]; return res; } Exp getExp(const string& S, int& p){ if(S[p] == '?'){ p++; return Exp{ {1,1,1} }; } if(S[p] == '0'){ p++; return Exp{ {1,0,0} }; } if(S[p] == '1'){ p++; return Exp{ {0,1,0} }; } if(S[p] == '2'){ p++; return Exp{ {0,0,1} }; } if(S[p] == 'm'){ p++; // m char nx = S[p]; p++; // ? p++; // x p++; // ( Exp a = getExp(S,p); p++; // , Exp b = getExp(S,p); p++; // ) if(nx == '?') return mexExp(a,b) + maxExp(a,b); if(nx == 'e') return mexExp(a,b); if(nx == 'a') return maxExp(a,b); } exit(1); } int main(){ string S; cin >> S; int p = 0; auto res = getExp(S,p); int k; cin >> k; cout << res.A[k].val() << endl; return 0; } struct ios_do_not_sync { ios_do_not_sync() { ios::sync_with_stdio(false); cin.tie(nullptr); } } ios_do_not_sync_instance;