#include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize("O3", "unroll-loops")

// #define FILE
using ll = long long;
const ll MOD = 998244353;
const ll A = 998244353, MOD1 = 1e9 + 7;

ll get_hash(vector<int>& a) {
    ll sum = 0;
    for (auto x : a) {
        sum = (sum * A + x) % MOD1;
    }
    return sum;
}

map<pair<ll, int>, vector<int>> mp;
map<ll, ll> ans;

ll pow1(ll n, int st) {
    if (st == 0) return 1;
    if (st % 2 == 1) return (pow1(n, st - 1) * n) % MOD;
    ll val = pow1(n, st / 2);
    return (val * val) % MOD;
}

ll calc(vector<int>& a) {
    ll sum = 0;
    for (int i = 0; i < a.size(); ++i) {
        sum = (sum + pow1(a[i], i + 1)) % MOD;
    }
    return sum;
}

vector<int> f(vector<int>& a, int t) {
    int n = a.size();
    vector<int> otv(a.size());
    if (t == 1) {
        for (int i = 1; i <= a.size(); ++i) {
            otv[i - 1] = a[n - i + 1 - 1];
        }
    } else if (t == 2) {
        for (int i = 0; i < a.size(); ++i) {
            otv[i] = n + 1 - a[i];
        }
    } else {
        for (int i = 0; i < a.size(); ++i) {
            otv[a[i] - 1] = i + 1;
        }
    }
    return otv;
}

signed main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef FILE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    int n, q; cin >> n >> q;
    vector<int> a(n);
    for (auto& x : a) cin >> x;
    ll now_hash = get_hash(a);
    while (q--) {
        int t; cin >> t;
        if (mp.find({now_hash, t}) == mp.end()) {
            auto vec = f(a, t);
            mp[{now_hash, t}] = vec;
            int hash1 = get_hash(vec);
            // cout << "new " << calc(vec) << endl;
            ans[hash1] = calc(vec);
        }
        // cout << "a1 ";
        // for (auto x : a) cout << x << " ";
        // cout << endl << t << " : ";
        //  for (auto x : mp[{a, t}]) cout << x << " ";
        // cout << endl;
        a = mp[{now_hash, t}];
        now_hash = get_hash(a);
        // cout << "a ";
        // for (auto x : a) cout << x << " ";
        // cout << endl;
        cout << ans[now_hash] << " ";
        // cout << endl;
    }
}