#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const ll mod = 998244353;

void make(vector<ll> &a) {
    ll n = (ll) a.size();
    for (auto &i : a) i = n - i + 1;
}

void pos(vector<ll> &a) {
    ll n = (ll) a.size();
    vector<ll> pos(n);
    for (ll i = 0; i < n; i++) {
        pos[a[i] - 1] = i + 1;
    }
    a = pos;
}

ll binpow(ll x, ll n) {
    if (n == 0) return 1;
    if (n == 1) return x;
    ll t = binpow(x, n / 2);
    t = (t * t) % mod;
    if (n % 2) t = (t * x) % mod;
    return t;
}

ll getsum(vector<ll> &a) {
    ll res = 0;
    ll n = (ll) a.size();
    for (ll i = 0; i < n; i++) {
        res = (res + binpow(a[i], i + 1)) % mod; 
    }
    return res;
}

int main() {
    ll n, m;
    cin >> n >> m;
    vector<ll> p(n);
    vector<vector<ll>> res1(2, vector<ll> (2)), res2(2, vector<ll> (2));
    for (auto &i : p) cin >> i;
    auto q = p;
    pos(q);
    for (ll i = 0; i < 2; i++) {
        for (ll j = 0; j < 2; j++) {
            auto a = p;
            if (i) reverse(a.begin(), a.end());
            if (j) make(a);
            res1[i][j] = getsum(a);
        }
    }
    for (ll i = 0; i < 2; i++) {
        for (ll j = 0; j < 2; j++) {
            auto b = q;
            if (i) reverse(b.begin(), b.end());
            if (j) make(b);
            res2[i][j] = getsum(b);
        }
    }
    bool c1 = 0, c2 = 0, c3 = 0;
    while (m--) {
        ll ord;
        cin >> ord;
        if (ord == 1) c1 = (c1 ^ 1);
        else if (ord == 2) c2 = (c2 ^ 1);
        else c3 = (c3 ^ 1);
        if (c3 == 0) {
            cout << res1[c1][c2] << ' ';
        } else {
            cout << res2[c1][c2] << ' ';
        }
    }
    cout << '\n';
}
