#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

const ll INF = (ll)1e18;
const ll MOD = (ll)998244353;

ll binpow(ll x, ll pw) {
    if (pw == 0) return 1;
    if (pw == 1) return x;
    if (pw % 2 == 0) {
        ll y = binpow(x, pw / 2);
        return y * y % MOD;
    }
    return binpow(x, pw - 1) * x % MOD;
}

ll get_ans(int n, vector<int> &p) {
    ll sm = 0;
    for (int i = 0; i < n; ++i) {
        sm += binpow(p[i], i + 1);
        sm %= MOD;
    }
    return sm;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, q;
    cin >> n >> q;
    vector<int> p(n), b(q);
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
    }
    for (int j = 0; j < q; ++j) {
        cin >> b[j];
    }
    if (max(n, q) <= 1000) {
        vector<ll> ans(q);
        for (int i = 0; i < q; ++i) {
            if (b[i] == 1) {
                reverse(p.begin(), p.end());
            } else if (b[i] == 2) {
                for (int j = 0; j < n; ++j) {
                    p[j] = n - p[j] + 1;
                }
            } else {
                vector<int> q(n);
                for (int j = 0; j < n; ++j) {
                    q[p[j] - 1] = j + 1;
                }
                p = q;
            }
            ll sm = 0;
            for (int j = 0; j < n; ++j) {
                sm += binpow(p[j], j + 1);
                if (sm >= MOD) {
                    sm -= MOD;
                }
            }
            ans[i] = sm;
        }
        for (ll el : ans) cout << el << " ";
        cout << "\n";
    } else {
        vector<ll> ans(4);
        ans[0] = get_ans(n, p);
        reverse(p.begin(), p.end());
        ans[2] = get_ans(n, p);
        for (int i = 0; i < n; ++i) {
            p[i] = n - p[i] + 1;
        }
        ans[3] = get_ans(n, p);
        reverse(p.begin(), p.end());
        ans[1] = get_ans(n, p);
        for (int i = 0; i < n; ++i) {
            p[i] = n - p[i] + 1;
        }
        int m1 = 0, m2 = 0;
        for (int i = 0; i < q; ++i) {
            if (b[i] == 1) {
                m1 ^= 1;
            } else {
                m2 ^= 1;
            }
            cout << ans[m1 * 2 + m2] << " ";
        }
        cout << "\n";
    }
    return 0;
}