MOD = 998244353


def binpow(a, power):
    if power == 0:
        return 1
    if power == 1:
        return a
    if power % 2 == 1:
        return binpow(a, power - 1) * a
    prev = binpow(a, power // 2)
    return prev * prev


n, q = map(int, input().split())
p = list(map(int, input().split()))
b = list(map(int, input().split()))

for op in b:
    sum = 0
    if op == 1:
        p = p[::-1]
    elif op == 2:
        t = [0] * n
        for i in range(n):
            t[i] = n - p[i] + 1
        p = t
    else:
        t = [0] * n
        for i in range(n):
            t[p[i] - 1] = i + 1
        p = t
    for i in range(n):
        sum = (sum + (binpow(p[i], i + 1) % MOD)) % MOD
    print(sum,end = " ")