#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;
}

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];
    }
    for (ll i = 0; i < q; i++) {
        cin >> query[i];
    }
    if (n * q <= 1e6) {
        for (ll e = 0; e < q; e++) {
            vector<ll> ne(n);
            if (query[e] == 1) {
                for (ll i = 0; i < n; i++) {
                    ne[i] = ls[n - i - 1];
                }
            } else if (query[e] == 2) {
                for (ll i = 0; i < n; i++) {
                    ne[i] = n - ls[i] + 1;
                }
            } else {
                for (ll i = 0; i < n; i++) {
                    ne[ls[i] - 1] = i + 1;
                }
            }
            ls = std::move(ne);
            ll ans = 0;
            for (ll i = 0; i < n; i++) {
                ans = (ans + step(ls[i], i + 1)) % mod;
            }
            cout << ans << " ";
        }
    }
}

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