MOD = 998244353


def fast_pow(a, b):
    if b == 1: return a
    if b == 0: return 1
    k = fast_pow(a, b // 2)
    return k * k % MOD * fast_pow(a, b % 2) % MOD


def get_answer(nums):
    ans = 0
    for i in range(len(nums)):
        ans = (ans + fast_pow(nums[i], i + 1)) % MOD
    return ans


n, q = map(int, input().split())
nums = [int(x) for x in input().split()]

char = [[]]
k = 2
char.append(nums);
get = [[] for i in range(9)]
get[1] = nums
answer = [0 for i in range(9)]
answer[1] = get_answer(nums)
perehodi = [[0 for i in range(4)] for i in range(9)]
now = 1
queryes = [int(x) for x in input().split()]
for j in range(q):
    t = queryes[j]
    if perehodi[now][t] == 0:
        nums = get[now]
        if t == 1:
            nums = [nums[n - i - 1] for i in range(n)]
        elif t == 2:
            nums = [n - nums[i] + 1 for i in range(n)]
        elif t == 3:
            new = [i for i in range(n)]
            for i in range(n):
                new[nums[i] - 1] = i + 1
            nums = new
        if nums not in char:
            char.append(nums)
            get[k] = nums
            answer[k] = get_answer(nums)
            k += 1
        perehodi[now][t] = char.index(nums)
    now = perehodi[now][t]
    print(answer[now])
