mod=998244353
n,q=map(int,input().split())
A=list(map(int,input().split()))
C=list(map(int,input().split()))
D=dict()
A0=list(A)
for v1 in range(2):
    for v2 in range(2):
        for v3 in range(2):
            A=list(A0)
            z=0
            if v1==1:
                A.reverse()
            if v2==1:
                for i in range(n):
                    A[i]=n-A[i]+1
            if v3==1:
                B=[0]*n
                for i in range(n):
                    B[A[i]-1]=i+1
                A=list(B)
            z=0
            for i in range(n):
                z=(z+pow(A[i],i+1,mod))%mod
            D[(v1,v2,v3)]=z
v1,v2,v3=0,0,0
for c in C:
    if c==1:
        v1+=1
        v1%=2
    elif c==2:
        v2+=1
        v2%=2
    else:
        v3+=1
        v3%=2
    print(D[(v1,v2,v3)],end=" ")




























    
