// dimaloh

#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;
using ll = long long;
using lll = __int128;
using pii = pair<int, int>;
using ld = long double;
using pll = pair<ll, ll>;

#define len(v) (int)v.size()
#define all(v) v.begin(), v.end()


template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const ll INF = 1e18;
const int inf = 1e9, mod = 998'244'353, sz = 5e5 + 10, logn = 20;
const ld pi = acosl(-1), eps = 1e-8;

mt19937 rnd(87438);

int bin_pow(int a, int b) {
    if (b == 0) return 1;
    if (b % 2) {
        return a * 1ll * bin_pow(a, b - 1) % mod;
    }
    int k = bin_pow(a, b / 2);
    return k * 1ll * k % mod;
}


int n;

int f(vector<int>&p) {
    int ans = 0;
    for (int i = 0; i < len(p); ++i) {
        ans = (ans + bin_pow(p[i], i + 1)) % mod;
    }
    return ans;
}

void rev(vector<int>&p) {
    for (int i = 0; i < n - i - 1; ++i) {
        swap(p[i], p[n - i - 1]);
    }
}

void flip(vector<int>&p) {
    for (int i = 0; i < n; ++i) {
        p[i] = n - p[i] + 1;
    }
}


void obr(vector<int>&p) {
    vector<int>q(n);
    for (int i = 0; i < n; ++i) {
        q[p[i] - 1] = i + 1;
    }
    p = q;
}

void print(vector<int>&v) {
    for (auto &i : v) cout << i << ' '; cout << endl;
}

void solve() {
    int q; cin >> n >> q;
    vector<int>p(n);
    for (auto &i : p) {
        cin >> i;
    }
    if (n <= 1'000 && q <= 1'000) {
        while (q--) {
            int b; cin >> b;
            if (b == 1) {
                rev(p);
            } else if (b == 2) {
                flip(p);
            } else {
                obr(p);
            }
            cout << f(p) << ' ';
        }
        return;
    }
    int mx1 = 3, mx2 = 3, mx3 = 3;
    int ans[mx1][mx2][mx3];

    auto p1 = p;
    for (int k = 0; k < mx3; ++k, obr(p1)) {
        for (int j = 0; j < mx2; ++j, flip(p1)) {
            for (int i = 0; i < mx1; ++i, rev(p1)) {
                ans[i][j][k] = f(p1);
            }
        }
    }

    int q1 = 0, q2 = 0, q3 = 0;
    while (q--) {
        int b; cin >> b;
        if (b == 1) q1++;
        else if (b == 2) q2++;
        else q3++;

        cout << ans[q1 % mx1][q2 % mx2][q3 % mx3] << ' ';
    }
}

signed main() {
    cin.tie(0)->sync_with_stdio(0);
    cout.precision(20);

    int t = 1;

#ifdef DEBUG
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    cin >> t;
#endif


    while (t--) {
        solve();
        cout << '\n';
    }
}