#include <bits/stdc++.h>

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

ll bp(ll a, ll b) {
    if (b == 0) return 1;
    ll ans = 1;
    if (b % 2) ans = a;
    ll tp = bp(a, b / 2);
    ans = ans * tp % mod * tp % mod;
    return ans;
}

void solve() {
    ll n, q;
    cin >> n >> q;
    vector<ll> p(n), b(q);
    for (ll& i : p) cin >> i;
    for (ll& i : b) {
        cin >> i;
    }
    ll cnt = 100;
    vector<vector<ll>> g(cnt, vector<ll>(4, -1));
    ll cur = 0;
    vector<ll> val(cnt, -1);
    val[cur] = 0;
    for (ll i = 0; i < n; i++) {
        val[0] += bp(p[i], i + 1);
        val[0] %= mod;
    }
    vector<vector<ll>> arr(cnt);
    arr[0] = p;
    map<ll, ll> idx;
    idx[val[0]] = 0;
    ll cn = 1;
    for (auto i : b) {
        if (g[cur][i] == -1) {
            auto nw = arr[cur];
            if (i == 1) {
                reverse(nw.begin(), nw.end());
            }
            else if (i == 2) {
                for (auto& i : nw) {
                    i = n - i + 1;
                }
            }
            else {
                for (ll i = 0; i < n; i++) {
                    nw[arr[cur][i] - 1] = i + 1;
                }
            }
            ll new_val = 0;
            for (ll i = 0; i < n; i++) {
                new_val += bp(nw[i], i + 1);
                new_val %= mod;
            }
            if (!idx.count(new_val)) {
                g[cur][i] = cn++;
                arr[g[cur][i]] = nw;
                idx[new_val] = g[cur][i];
            }
            else {
                g[cur][i] = idx[new_val];
            }
            val[g[cur][i]] = new_val;
        }
        cur = g[cur][i];
        cout << val[cur] << " ";
    }
}

signed main() {
    solve();
}