#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define all(x) x.begin(), x.end()
using ll = long long;
using ui = unsigned int;
using ull = unsigned long long;
using ld = long double;
template<typename T> 
using orset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const ll mod = 998244353;

ll stmod(ll a, ll st) {
    if (st == 0) return 1;
    if (st & 1) return stmod(a, st - 1) * a % mod;
    ll hf = stmod(a, st / 2);
    return hf * hf % mod;
}

int main () {
    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll n, q;
    cin >> n >> q;
    vector<int> p(n);
    for (int i = 0; i < n; i++) {
        cin >> p[i];
        p[i]--;
    }
    for (int t = 0; t < q; t++) {
        int tp;
        cin >> tp;
        if (tp == 1) {
            for (int i = 0; i < n / 2; i++) {
                swap(p[i], p[n - i - 1]);
            }
        } else if (tp == 2) {
            for (int i = 0; i < n; i++) {
                p[i] = n - p[i] - 1;
            }
        } else {
            vector<int> nv(n);
            for (int i = 0; i < n; i++) {
                nv[p[i]] = i;
            }
            swap(nv, p);
        }
        ll ans = 0;
        for (int i = 0; i < n; i++) {
            ans += stmod(p[i] + 1, i + 1);
            ans -= (ans >= mod? mod : 0);
        }
        cout << ans << " ";
    }
}
