#include <bits/stdc++.h>

using ll = long long;

const ll INF = 2e9;
const ll MOD = 1e9 + 7;
const ll mod = 998244353;

using namespace std;

vector<int> primes;

void getpr() {
    vector<bool> is(1e6 + 1, 1);
    for (ll i = 2; i <= 1e6; i++) {
        if (!is[i]) {
            continue;
        }
        for (ll j = i * i; j <= 1e6; j += i) {
            is[j] = 0;
        }
    }
    for (int i = 2; i <= 1e6; i++) {
        if (is[i]) {
            primes.push_back(i);
        }
    }
}

ll bin_pow(ll x, ll p) {
    if (p == 0) {
        return 1;
    }
    ll v = bin_pow(x * x % mod, p / 2);
    if (p & 1 == 1) {
        v *= x;
    }
    return v % mod;
}

map<ll, vector<int>> rev;
vector<map<ll, ll>> g1(3);
map<ll, int> ans;
int SZ;

ll hsh = 0, n;

ll counthash(vector<int>& v) {
    ll k = 0;
    for (int i = 0; i < n; i++) {
        k += bin_pow(primes[v[i] % SZ], v[i] + i + 2);
    }
    return k % mod;
}

ll countans(vector<int>& v) {
    ll k = 0;
    for (int i = 0; i < n; i++) {
        k += bin_pow(v[i] + 1, i + 1);
    }
    return k % mod;
}

void change(vector<int>& p, int x) {
    if (g1[x].find(hsh) == g1[x].end()) {
        p = rev[hsh];
        vector<int> v = p;
        if (x == 0) {
            reverse(v.begin(), v.end());
        }
        if (x == 1) {
            for (auto& x : v) {
                x = n - 1 - x;
            }
        }
        if (x == 2) {
            vector<int> q(n);
            for (int i = 0; i < n; i++) {
                q[v[i]] = i;
            }
            swap(q, v);
        }
        ll hsh1 = counthash(v);
        ans[hsh1] = countans(v);
        g1[x][hsh] = hsh1;
        rev[hsh1] = v;
        hsh = hsh1;
    } else {
        hsh = g1[x][hsh];
    }
}

void solve() {
    getpr();
    SZ = primes.size();
    int q;
    cin >> n >> q;
    vector<int> p(n);
    for (int i = 0; i < n; i++) {
        cin >> p[i];
        p[i]--;
    }
    hsh = counthash(p);
    ans[hsh] = countans(p);
    rev[hsh] = p;
    for (int i = 0; i < q; i++) {
        int x;
        cin >> x;
        x--;
        change(p, x);
        cout << ans[hsh] << ' ';
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t = 1;
    bool MULTITEST = 0;
    #ifdef egg
        MULTITEST = 1;
        freopen("test.txt", "r", stdin);
    #endif
    if (MULTITEST) {
        cin >> t;
    }
    while (t--) {
        solve();
    }
    return 0;
}
