#include #include using namespace std; using namespace atcoder; using mint = modint998244353; int mex(int x, int y) { if( x > y ) swap(x, y); if( x == 0 ) { if( y == 1 ) { return 2; }else { return 1; } }else { return 0; } } bool isnumber(char c) { return ( c == '?' || isdigit(c) ); } vector number(string& S, int& i) { vector dp(3); for( int j = 0; j < 3; j++ ) { if( S[i] == '?' || S[i]-'0' == j ) dp[j]++; } i += 1; return dp; } vector expression(string& S, int& i) { vector dp(3), lp(3), rp(3); if( isnumber(S[i]) ) { return number(S, i); }else { char op = S[i+1]; i += 4; lp = expression(S, i); i += 1; rp = expression(S, i); for( int j = 0; j < 3; j++ ) { for( int k = 0; k < 3; k++ ) { if( op != 'e' ) dp[max(j, k)] += lp[j]*rp[k]; if( op != 'a' ) dp[mex(j, k)] += lp[j]*rp[k]; } } } i += 1; return dp; } int main() { int K, i = 0; string S; cin >> S >> K; cout << expression(S, i)[K].val() << endl; }