mod = 998244353

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


def rev(t):
    global p
    if t == 1:
        p = p[::-1]
    if t == 2:
        for i in range(n):
            p[i] = n - p[i] + 1
    if t == 3:
        q = [0 for i in range(n)]
        for i in range(n):
            q[p[i] - 1] = i + 1
        p = q


mod = 998244353


def bpow(a, n):
    if n == 0:
        return 1
    if n % 2 == 0:
        c = bpow(a, n // 2)
        return (c * c) % mod
    return (a * bpow(a, n - 1)) % mod


def calc():
    c = 0
    for i in range(n):
        c = (c + bpow(p[i], i + 1)) % mod
    return c


s = set()
for i in range(2):
    for j in range(2):
        for k in range(2):
            s.add(tuple(p))
            rev(3)
        rev(2)
    rev(1)

s = list(s)
ans = [0 for i in range(len(s))]

g = [[] for i in range(len(s))]
for i in range(len(s)):
    p = list(s[i])
    ans[i] = calc()
    for j in range(3):
        rev(j + 1)
        g[i].append(s.index(tuple(p)))
        rev(j + 1)

cur = s.index(tuple(orig))
for i in range(q):
    cur = g[cur][b[i] - 1]
    print(ans[cur], end=" ")
