#pragma GCC optimize("O3,unroll-loops")
#include <vector>
#pragma GCC target("avx,avx2")
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using dbl = long double;

const ll P = 998244353;

ll pw(ll a, ll b) {
    if (b == 0) return 1;
    if (b % 2) return pw(a, b - 1) * a % P;
    ll x = pw(a, b / 2);
    return x * x % P;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    int n, q;
    cin >> n >> q;

    array<vector<int>, 8> v;
    v.fill(vector<int>(n));
    for (auto& i : v[0]) cin >> i;
    for (auto& i : v[0]) i--;
    for (int i = 0; i < n; i++) {
        v[1][v[0][i]] = i;
    }
    for (int j = 2; j < 8; j++) {
        bool digr = j & 1;
        bool horr = j & 2;
        bool verr = j & 4;
        for (int i = 0; i < n; i++) {
            int trg = v[digr][i];
            if (horr) trg = v[digr][n - i - 1];
            if (verr) trg = n - trg - 1;
            v[j][i] = trg;
        }
    }
    array<ll, 8> val;
    val.fill(0);
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < n; j++) {
            val[i] = (val[i] + pw(v[i][j] + 1, j + 1)) % P;
        }
    }
    int tp = 0;
    for (int i = 0; i < q; i++) {
        int x;
        cin >> x;
        if (x == 1) tp ^= 2;
        else if (x == 2) tp ^= 4;
        else {
            int ntp = (tp & 1) ^ 1;
            ntp += (tp & 2) << 1;
            ntp += (tp & 4) >> 1;
            tp = ntp;
        }
        cout << val[tp] << " ";
    }
}