#include <bits/stdc++.h>
#pragma optimize "O3"
#pragma optimize "fast-math"
using namespace std;
#define int long long
using ll = long long;
using vint = vector<int>;
using pii = pair<int, int>;
const int INF = 1 << 30;
const int MOD = 998244353;

int Func(int x,int y) {
    if (y == 1) {
        return x;
    }
    if (y == 0) {
        return 1;
    }
    int a = Func(x, y / 2);
    if (y % 2 == 0) {
        return (a * a) % MOD;
    }
    return ((a * a) % MOD * x) % MOD;
}

int count1(const vint &v) {
    int ans = 0;
    int a = 1;
    for (int x: v) {
        ans = (ans + Func(x, a)) % MOD;
        a++;
    }
    return ans;
}

int n;

vint rev1(vint v) {
    vint ans;
    for (int i = n - 1; i >= 0; i--) {
        ans.push_back(v[i]);
    }
    return ans;
}

vint rev2(vint v) {
    vint ans(n);
    for (int i = n - 1; i >= 0; i--) {
        ans[i] = n - v[i] + 1;
    }
    return ans;
}

vint rev3(vint v) {
    vint ans(n);
    for (int i = n - 1; i >= 0; i--) {
        ans[v[i] - 1] = i + 1;
    }
    return ans;
}


signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int q;
    cin >> n >> q;
    vint v1(n);
    for (int i = 0; i < n; i++) {
        cin >> v1[i];
    }
    while (q--) {
        int t;
        cin >> t;
        if (t == 1) {
            v1 = rev1(v1);
        }
        if (t == 2) {
            v1 = rev2(v1);
        }
        if (t == 3) {
            v1 = rev3(v1);
        }
        cout << count1(v1) << ' ';
    }
}

//
//5 10
//3 1 1 2 2
