def convolute(a,b,op):
    c=[0,0,0]

    for i in range(3):
        for j in range(3):
            c[op[i][j]]+=a[i]*b[j]

    for i in range(3):
        c[i]%=Mod
    return c

S=input()
K=int(input())

MAX=[[max(x,y) for x in [0,1,2]] for y in [0,1,2]]
MEX=[[1,2,1],[2,0,0],[1,0,0]]

if len(S)==1:
    if S=="?":
        print(1)
    else:
        if int(S)==K:
            print(1)
        else:
            print(0)
    exit()

def three_pop():
    T=[]
    for _ in range(3):
        T.append(X.pop())
    return T[::-1]

from collections import deque
Mod=998244353
i=0
X=deque([])
while i<len(S):
    if S[i]=="m":
        X.append(S[i:i+3])
        i+=3
    elif S[i]=="(":
        i+=1
    elif S[i]==",":
        i+=1
    elif S[i]==")":
        op,a,b=three_pop()
        if op[1]=="a":
            X.append(convolute(a,b,MAX))
        elif op[1]=="e":
            X.append(convolute(a,b,MEX))
        else:
            c=convolute(a,b,MAX)
            d=convolute(a,b,MEX)
            e=[(x+y)%Mod for x,y in zip(c,d)]
            X.append(e)
        i+=1
    else:
        if S[i]=="?":
            X.append([1,1,1])
        else:
            T=[0,0,0]
            T[int(S[i])]=1
            X.append(T)
        i+=1

print(X[0][K])