#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;

// #pragma GCC optimize("O3")
// #pragma GCC target("avx2")
// #pragma GCC optimize("unroll-loops")

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const ll mod = 998244353;

ll step(ll x, ll y) {
    if (y == 0) return 1;
    ll ans = step(x, y / 2);
    ans = (ans * ans) % mod;
    if (y % 2 == 1) {
        ans = (ans * x) % mod;
    }
    return ans;
}

ll p = 239;

ll ha(vector<ll> &ls) {
    ll ans = 0;
    ll p1 = p;
    for (ll i = 0; i < ls.size(); i++) {
        ans = (ans + ls[i] * p1) % mod;
        p1 = (p1 * p) % mod;
    }
    return ans;
}

void solve() {
    ll n, q;
    cin >> n >> q;
    vector<ll> ls(n);
    vector<ll> query(q);
    for (ll i = 0; i < n; i++) {
        cin >> ls[i];
    }
    ll ma = 0;
    set<ll> st;
    for (ll i = 0; i < q; i++) {
        cin >> query[i];
        ma = max(ma, query[i]);
        st.insert(query[i]);
    }
    map<pair<ll, ll>, ll> mp;
    map<ll, ll> res;
    map<ll, vector<ll> > mp1;
    ll have = ha(ls);
    mp1[have] = ls;
    for (ll i = 0; i < q; i++) {
        if (mp[{have, query[i]}] != 0) {
            have = mp[{have, query[i]}];
            cout << res[have] << ' ';
        } else {
            ls = mp1[have];
            vector<ll> ne(n);
            if (query[i] == 1) {
                for (ll j = 0; j < n; j++) {
                    ne[j] = ls[n - j - 1];
                }
            } else if (query[i] == 2) {
                for (ll j = 0; j < n; j++) {
                    ne[j] = n - ls[j] + 1;
                }
            } else {
                for (ll j = 0; j < n; j++) {
                    ne[ls[j] - 1] = j + 1;
                }
            }
            ll h1 = ha(ne);
            ll ans = 0;
            for (ll j = 0; j < n; j++) {
                ans = (ans + step(ne[j], j + 1)) % mod;
            }
            mp[{have, query[i]}] = h1;
            mp1[h1] = ne;
            res[h1] = ans;
            have = h1;
            cout << ans << ' ';
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
}
