import random


def f(a, i):
    if i == 0:
        return a
    if i == 1:
        return a[::-1]
    if i == 2:
        return [len(a) + 1 - e for e in a]
    b = [0] * len(a)
    for i in range(len(a)):
        b[a[i] - 1] = i + 1
    return b

MOD = 998244353

def pw(a, b):
    if b == 0:
        return 1
    if b % 2 == 1:
        return (pw(a, b - 1) * a) % MOD
    t = pw(a, b // 2)
    return (t * t) % MOD

def g(a):
    ans = 0
    for i in range(len(a)):
        ans = (ans + pw(a[i], i + 1)) % MOD
    return ans

n, q = map(int, input().split())
a = list(map(int, input().split()))
bb = list(map(int, input().split()))
'''
a = list(range(1, 67))
random.shuffle(a)
'''
svo = [[0,0,0],[0, 0, 1],
[0, 0, 2],
[0, 0, 3],
[0, 1, 2],
[0, 1, 3],
[0, 2, 3],
[1, 2, 3]]
ans = []
b1 = a.copy()
for e in svo:
    b4 = f(f(f(b1, e[0]), e[1]), e[2])
    if b4 not in ans:
        ans.append(b4)

#print(len(ans))
#print(*ans, sep="\n")

X = len(ans)
res = dict()
for i in range(X):
    res[i] = g(ans[i])

dp = [[0 for i in range(4)] for j in range(X)]
for e in ans:
    for i in range(4):
        b = f(e, i)
        dp[ans.index(e)][i] = ans.index(b)
#print(dp)
now = ans.index(a)
for e in bb:
    #print(now, e)
    now = dp[now][e]
    print(res[now], end=" ")
print()