#include <bits/stdc++.h>

using namespace std;
using ll = long long;
vector<ll> v;
const ll mod = 998244353;

ll pw(ll x, ll y) {
    ll ans = 1;
    while (y > 0) {
        ll r = x, k = 1;
        while (k * 2 <= y) {
            r = (r * r) % mod;
            k *= 2;
        }
        y -= k;
        ans = (ans * r) % mod;
    }
    return ans;
}

signed main() {
    ll n, q;
    cin >> n >> q;
    v.resize(n);
    for (ll i = 0; i < n; i++) cin >> v[i];
    vector<ll> answer(8);
    for (ll i = 0; i < 8; i++) {
        vector<ll> p = v;
        if (i & 1) for (ll i = 0; i < n / 2; i++) swap(p[i], p[n - i - 1]);
        if (i & 2) for (ll i = 0; i < n; i++) p[i] = n - v[i] + 1;
        if (i & 4) for (ll i = 0; i < n; i++) p[v[i] - 1] = i + 1;
        ll ans = 0;
        for (ll i = 0; i < n; i++) ans = (ans + pw(p[i], i + 1)) % mod;
        answer[i] = ans;
    }
    ll cnt = 0;
    while (q--) {
        ll b;
        cin >> b;
        if (b == 1 && !(cnt & 1)) cnt ^= 1;
        if (b == 2 && !(cnt & 2)) cnt ^= 2;
        if (b == 3 && !(cnt & 4)) cnt ^= 4;
        cout << answer[cnt] << ' ';
    }
}
