#include<bits/stdc++.h>
#define ll long long
#define ld long double

using namespace std;

ll n, q, MOD = 998244353;
vector<ll> p, b;

ll BVS(ll a, ll b) {
    if (b == 0) {
        return 1;
    } else if (b % 2 == 0) {
        return BVS((a * a) % MOD, b / 2);
    } else {
        return (BVS(a, b - 1) * a) % MOD;
    }
}

void input() {
    cin >> n >> q;
    p.resize(n);
    b.resize(q);
    for (ll i = 0; i < n; i++) {
        cin >> p[i];
    }
    for (ll i = 0; i < q; i++) {
        cin >> b[i];
    }
}

void fir() {
    vector<ll> ret(n);
    for (ll i = 0; i < n; i++) {
        ret[i] = p[n - i - 1];
    }
    p = ret;
}

void sec() {
    vector<ll> ret(n);
    for (ll i = 0; i < n; i++) {
        ret[i] = n - p[i] + 1;
    }
    p = ret;
}

void thir() {
    vector<ll> ret(n);
    for (ll i = 0; i < n; i++) {
        ret[p[i] - 1] = i + 1;
    }
    p = ret;
}

void solve() {
    for (ll i = 0; i < q; i++) {
        if (b[i] == 1) {
            fir();
        } else if (b[i] == 2) {
            sec();
        } else {
            thir();
        }
        //for (ll i = 0; i < p.size(); i++) {
        //    cout << p[i] << " ";
        //}
        //cout << "\n";
        ll sum = 0;
        for (ll i = 0; i < n; i++) {
            sum += BVS(p[i], i + 1);
            sum %= MOD;
        }
        cout << sum << " ";
    }
}

void output() {

}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    input();
    solve();
    output();
    return 0;
}
