#include <bits/stdc++.h>
#include <random>

#define i128 __int128
#define int long long

using namespace std;

random_device lal;
mt19937 rnd(lal());


const int inf = 1e18, mod = 998'244'353;
const int sg_base = 524288, sg_size = sg_base * 2 - 1;

int binpow(int num, int power) {
    if (power == 0) { return 1; }
    if (power % 2 == 0) {
        int res = binpow(num, power / 2);
        return (res * res) % mod;
    }
    return (binpow(num, power - 1) * num) % mod;
}


vector<int> do1(vector<int>p) {
    for (int i = 0; i < p.size() / 2; ++i) {
        swap(p[i], p[p.size() - i - 1]);
    }
    return p;
}

vector<int> do2(vector<int>p) {
    for (int i = 0; i < p.size(); ++i) {
        p[i] = p.size() - p[i] + 1;
    }
    return p;
}

vector<int> do3(vector<int>p) {
    int n = p.size();
    vector<int> p2(n);
    for (int i = 0; i < n; ++i) {
        p2[p[i] - 1] = i + 1;
    }
    return p2;
}

int calc(vector<int>p) {
    int ans = 0;
    for (int i = 0; i < p.size(); ++i) {
        ans += binpow(p[i], i + 1);
        ans%=mod;
    }
    return ans;
}

void print(vector<int> &a) {
    for (int i: a){cout << i << ' ';}
    cout<<'\n';
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, q;
    cin >> n >> q;
    vector<int> p(n) ,b(q);
    for (int &i: p) { cin >> i; }
    for (int &i: b) { cin >> i; }

    vector<int>answers(8, -1);
    int cur = 0;
    for (int i = 0; i < q; ++i) {
        if (b[i]==1) {
            cur ^= 1;
        }else if (b[i]==2) {
            cur^=2;
        }else {
            cur^=4;
        }
        if (answers[cur]==-1) {
            auto j = p;
            if (cur&4) {
                j = do3(j);
            }
            if ((cur&1)) {
                j = do1(j);
            }
            if (cur&2) {
                j = do2(j);
            }
            
            answers[cur] = calc(j);
        }
        cout<<answers[cur] << ' ';
    }
}
