#include //eolibraries #define lnf 3999999999999999999 #define inf 999999999 #define fi first #define se second #define pb push_back #define all(c) (c).begin(),(c).end() #define sz(c) (int)(c).size() #define make_unique(a) sort(all(a)),a.erase(unique(all(a)),a.end()); #define rep(i,n) for(int i=0;i=0;i--) #define crep(i,x,n) for(int i=x;i #define _3E0svGb ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0) //eodefine using namespace std; typedef long long ll; typedef long double ld; using pii=pair; using tpii=pair; using vi=vec(int); const int mod=998244353; //snuke's modular int struct modularint{ ll x; modularint(ll x=0):x(x%mod){} modularint& operator+=(const modularint a){ if ((x += a.x) >= mod) x -= mod; return *this; } modularint& operator-=(const modularint a){ if ((x += mod-a.x) >= mod) x -= mod; return *this; } modularint& operator*=(const modularint a){ (x *= a.x) %= mod; return *this; } modularint operator+(const modularint a)const{ modularint res(*this); return res+=a; } modularint operator-(const modularint a)const{ modularint res(*this); return res-=a; } modularint operator*(const modularint a)const{ modularint res(*this); return res*=a; } modularint pow(ll n)const{ modularint res=1,x(*this); while(n){ if(n&1)res*=x; x*=x; n>>=1; } return res; } modularint inv()const{ return pow(mod-2); } }; using mint=modularint; int main(){ _3E0svGb; string s; cin>>s; int i=0; auto mex=[&](int x,int y){ rep(i,3) if(x!=i and y!=i) return i; }; auto f=[&](auto self)->vec(mint){ vec(mint) now(3); if(s[i]=='?' or (s[i]>='0' and s[i]<='9')){ if(s[i]=='?') rep(t,3) now[t]=1; else now[int(s[i]-'0')]=1; }else{ string h=s.substr(i,3); i+=4; vec(mint) l=self(self); i+=2; vec(mint) r=self(self); i++; if(h!="max"){ rep(i,3) rep(j,3) now[mex(i,j)]+=l[i]*r[j]; } if(h!="mex"){ rep(i,3) rep(j,3) now[max(i,j)]+=l[i]*r[j]; } } return now; }; vec(mint) res=f(f); int k; cin>>k; printf("%lld\n",res[k].x); // return 0; }