#include<bits/stdc++.h>
// #include<ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/tree_policy.hpp>

#define int long long
const int INF = 1e18, MOD = 998244353;

using namespace std;
// using namespace __gnu_pbds;

// #define oset tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>

vector<int> fw;

void upd(int pos, int val) {
    for (; pos < (int)fw.size(); pos = (pos | (pos + 1))) {
        fw[pos] += val;
    }
}


int get(int r) {
    int ans = 0;
    for (; r >= 0; r = (r & (r + 1)) - 1) {
        ans += fw[r];
    }
    return ans;
}


vector<int> tree;

void push(int v) {}

void update(int v, int l, int r, int ql, int qr, int val) {
    push(v);
    if (l >= ql && r <= qr) {
        // 
    } else if (l >= qr || r <= ql) {
        return;
    } else {
        int m = (l + r) / 2;
        update(2 * v + 1, l, m, ql, qr, val);
        update(2 * v + 2, m, r, ql, qr, val);
        tree[v] = min(tree[2 * v + 1], tree[2 * v + 2]);
    }
}

int get(int v, int l, int r, int ql, int qr) {
    push(v);
    if (l >= ql && r <= qr) {
        return tree[v];
    } else if (l >= qr || r <= ql) {
        return INF;
    } else {
        int m = (l + r) / 2;
        return min(get(2 * v + 1, l, m, ql, qr), get(2 * v + 2, m, r, ql, qr));
    }
}

int fst(int a, int n) {
    if (!n) {
        return 1ll;
    } else if (n & 1) {
        return (a * fst(a, n - 1)) % MOD;
    } else {
        int m = fst(a, n / 2);
        return (m * m) % MOD;
    }
}


int calc(vector<int>& p) {
    int ans = 0, n = (int)p.size();
    for (int i = 0; i < n; i++) {
        ans = (ans + fst(p[i] + 1, i + 1)) % MOD;
    }
    return ans;
}


vector<int> rev(vector<int>& p) {
    vector<int> ans((int)p.size());
    for (int i = 0; i < (int)p.size(); i++) {
        ans[p[i]] = i;
    }
    return ans;
}

signed main() {
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    // oset st;
    int n, q;
    cin >> n >> q;
    vector<int> st1(n);
    for (int i = 0; i < n; i++) {
        cin >> st1[i];
        st1[i]--;
    }
    // auto uu = rev(st1);
    // for (auto u : uu) cout << u << ' ';
    // cout << '\n';
    // for (int i = 0; i < n; i++) uu[i] = n - uu[i] - 1;
    // reverse(uu.begin(), uu.end());
    // auto uu1 = rev(uu);
    // for (auto u : uu1) cout << u << ' ';
    // cout << '\n';
    auto s = st1;
    vector<int> ans(8);
    ans[0] = calc(st1);
    reverse(st1.begin(), st1.end());
    ans[1] = calc(st1);
    reverse(st1.begin(), st1.end());
    for (int i = 0; i < n; i++) {
        st1[i] = n - st1[i] - 1;
    }
    ans[2] = calc(st1);
    reverse(st1.begin(), st1.end());
    ans[3] = calc(st1);
    s = rev(s);
    ans[4] = calc(s);
    for (int i = 0; i < n; i++) {
        s[i] = n - 1 - s[i];
    }
    ans[5] = calc(s);
    for (int i = 0; i < n; i++) {
        s[i] = n - 1 - s[i];
    }
    reverse(s.begin(), s.end());
    ans[6] = calc(s);
    for (int i = 0; i < n; i++) {
        s[i] = n - 1 - s[i];
    }
    ans[7] = calc(s);
    swap(ans[5], ans[6]);
    int cur = 0;
    for (int i = 0; i < q; i++) {
        int b;
        cin >> b;
        if (b == 1) {
            cur ^= 1;
        } else if (b == 2) {
            cur ^= 2;
        } else {
            if (cur == 0 || cur == 4 || cur == 7 || cur == 3) {
                cur ^= 4;
            } else {
                cur = 7 - cur;
            }
        }
        cout << ans[cur] << '\n';
    }
}