#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
#define int long long

const int MOD = 998244353;

int fast_pow(int a, int b) {
    if (b == 0) return 1;
    if (b == 1) return a % MOD;
    int k = fast_pow(a, b / 2);
    return k * k * fast_pow(a, b % 2) % MOD;
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, q;
    cin >> n >> q;
    vector<int> nums(n + 1, 0);
    for (int i = 1; i <= n; i++) cin >> nums[i];

    while (q--) {
        int t;
        cin >> t;
        vector<int> update(n + 1, 0);
        if (t == 1) {
            for (int i = 1; i <= n; i++) {
                update[i] = nums[n - i + 1];
            }
        } else if (t == 2) {
            for (int i = 1; i <= n; i++) {
                update[i] = n - nums[i] + 1;
            }
        } else if (t == 3) {
            for (int i = 1; i <= n; i++) {
                update[nums[i]] = i;
            }
        }
        nums = update;
        int output = 0;
        for (int i = 1; i <= nums.size(); i++) {
            output = (output + fast_pow(nums[i], i)) % MOD;
        }
        cout << output << '\n';
    }
}
