#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 998244353;

int fast_pow(int x, int y) {
    if (y == 0) return 1;
    if (y == 1) return x;
    int x1 = fast_pow(x, y/2);
    if (y%2 == 0) return (ll(x1) * x1) % MOD;
    return (((ll(x1) * x1) % MOD) * x) % MOD;
}

void solve1(int n, int q, vector<int>&a, vector<int>&b) {
    for (int i = 0; i < q; i++) {
        if (b[i] == 1) {
            reverse(a.begin(), a.end());
        }
        if (b[i] == 2) {
            for (int j = 0; j < n; j++) {
                a[j] = n-a[j]+1;
            }
        }
        if (b[i] == 3) {
            vector<int>a1 = a;
            for (int j = 0; j < n; j++) {
                a[a1[j]-1] = j+1;
            }
        }
        int ans = 0;
        for (int j = 0; j < n; j++) {
            ans += fast_pow(a[j], j+1);
            ans %= MOD;
        }
        cout << ans << " ";
    } cout << endl;
}

void solve2(int n, int q, vector<int>&a, vector<int>&b) {
    vector<int>a1(n);
    if (b[0] == 1) {
        for (int i = 0; i < n; i++) {
            a1[i] = a[n-i-1];
        }
    }
    if (b[0] == 2) {
        for (int i = 0; i < n; i++) {
            a1[i] = n-a[i]+1;
        }
    }
    if (b[0] == 3) {
        for (int i = 0; i < n; i++) {
            a1[a[i]-1] = i+1;
        }
    }
    int ans1 = 0, ans0 = 0;
    for (int i = 0; i < n; i++) {
        ans1 += fast_pow(a1[i], i+1);
        ans1 %= MOD;
    }
    for (int i = 0; i < n; i++) {
        ans0 += fast_pow(a[i], i+1);
        ans0 %= MOD;
    }
    for (int i = 1; i <= q; i++) {
        if (i%2 == 1) cout << ans1 << " ";
        else cout << ans0 << " ";
    } cout << endl;
}

void solve3(int n, int q, vector<int>&a, vector<int>&b) {
    cout << 1 << endl;
}

int main() {
    int n, q; cin >> n >> q;
    vector<int>a(n); for (int i = 0; i < n; i++) cin >> a[i];
    vector<int>b(q); for (int i = 0; i < q; i++) cin >> b[i];
    solve1(n, q, a, b);
    return 0;
}