MOD = 998244353

def apply(b, v):
    n = len(v)
    v_ = [0]*n
    for i in range(n):
        v_[i] = v[i]
    if (b == 1):
        v = v[::-1]
    elif (b == 2):
        for i in range(n):
            v[i] = n - v_[i] + 1
    else:
        for i in range(n):
            v[v[i] - 1] = i + 1
    #print(*v)
    return v


def cost(v):
    n = len(v)
    ans = 0
    for i in range(n):
        ans += (pow(v[i], i + 1, MOD))
        #print(v[i], i + 1, ans)
    return ans

n, q = map(int, input().split())
v = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
for b in B:
    v = apply(b, v)
    print(cost(v), end=' ')

