#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 998244353;
map<vector<int>, int> mp;
int bin_pow(int a, int n) {
    if (n == 0) return 1;
    if (n % 2 == 0) {
        int x = bin_pow(a, n / 2);
        return (x * x) % mod;
    }
    return (a * bin_pow(a, n - 1)) % mod;
}
void upd1(vector<int> &c) {
    reverse(c.begin(), c.end());
}
void upd2(vector<int> &c) {
    int n = c.size();
    for (int i = 0; i < n; i++) {
        c[i] = n - c[i] + 1;
    }
}
void upd3(vector<int> &c) {
    int n = c.size();
    vector<int> d(n);
    for (int i = 0; i < n; i++) {
        d[c[i] - 1] = i + 1;
    }
    c = d;
}
int culc(vector<int> &c) {
    if (mp.find(c) != mp.end()) {
        return mp[c];
    }
    int ans = 0;
    int n = c.size();
    for (int i = 0; i < n; i++) {
        ans = (ans + bin_pow(c[i], i + 1)) % mod;
    }
    mp[c] = ans;
    return ans;
}
signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int n,q;
    cin >> n >> q;
    vector<int> a(n), b(q);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 0; i < q; i++) {
        cin >> b[i];
    }
    vector<int> c = a;
    for (int i = 0; i < q; i++) {
        if (b[i] == 1) {
            upd1(c);
        }else if (b[i] == 2) {
            upd2(c);
        }else {
            upd3(c);
        }
        int ans = culc(c);
        if (mp.size() > 5) return 0;
        cout << ans << " ";
    }
    cout << '\n';
}