#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
const int INF = 998244353;

int p(int x, int y) {
    if (y == 0) return 1;
    int h = p(x, y / 2) % INF;
    if (y % 2 == 0) return (h * h) % INF;
    return (((h * h) % INF) * x) % INF;
}

int cnt(vector<int> &a) {
    int ans = 0;
    for (int i = 0; i < a.size(); ++i) ans += p(a[i], i + 1), ans %= INF;
    return ans;
}

void solve() {
    int n, q;
    cin >> n >> q;
    vector<int> a(n);
    for (int i = 0; i < n; ++i) cin >> a[i];
    int ans[2][2][2];

    ans[0][0][0] = cnt(a);
    for (int i = 0; i < n; ++i) a[i] = n - a[i] + 1;
    ans[0][1][0] = cnt(a);
    for (int i = 0; i < n; ++i) a[i] = n - a[i] + 1;

    reverse(all(a));

    ans[1][0][0] = cnt(a);

    for (int i = 0; i < n; ++i) a[i] = n - a[i] + 1;

    ans[1][1][0] = cnt(a);

    vector<int> b(n);
    for (int i = 0; i < n; ++i) b[a[i] - 1] = i + 1;
    a = b;

    ans[1][1][1] = cnt(a);

    for (int i = 0; i < n; ++i) a[i] = n - a[i] + 1;

    ans[1][0][1] = cnt(a);

    reverse(all(a));

    ans[0][0][1] = cnt(a);

    for (int i = 0; i < n; ++i) a[i] = n - a[i] + 1;

    ans[0][1][1] = cnt(a);

    b.resize(n, 0);
    for (int i = 0; i < n; ++i) b[a[i] - 1] = i + 1;
    a = b;


    int i = 0;
    int j = 0;
    int k = 0;
    vector<int> c;
    while (q--) {
        int x;
        cin >> x;
        if (x == 1) {
            i ^= 1;
            c.push_back(ans[i][j][k]);
        }
        if (x == 2) {
            j ^= 1;
            c.push_back(ans[i][j][k]);
        }
        if (x == 3) {
            k ^= 1;
            c.push_back(ans[i][j][k]);
        }

    }
    for (auto i:c) cout << i << ' ';
    cout << '\n';
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    freopen("input.txt", "r", stdin);
    int t = 1;
    while (t--) solve();
}


