
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll INF = 1e15;
const ll mod = 998244353;


struct node{
    ll i, j;
};
vector<node> p;
ll fast_pow (ll x, ll k){
    if (k == 0) return 1;
    if (k % 2 == 0){
        ll d = fast_pow(x, k/2);
        return d*d%mod;
    }
    else{
        ll d = fast_pow(x, k/2);
        return d*d%mod*x%mod;
    }
}
ll n, q;

ll cnt (vector<node> &f) {
    ll ans = 0;
    for (int i = 0; i < n; ++i) {
        ans += fast_pow(f[i].i, f[i].j);
        ans %= mod;
    }
    return ans;
}


signed main() {
    ios_base::sync_with_stdio(false), cin.tie(0);
    cin >> n >> q;
    p.assign(n, {0, 0});
    for (int i = 0; i < n; ++i) {
        cin >> p[i].i;
        p[i].j = i+1;
    }
    vector<vector<node>> ver (8, vector<node> (n));
    vector<ll> mem (8);
    for (int i = 0; i < n; ++i) {
        ver[0][i] = {p[i].i, p[i].j};
    }
    for (int i = 0; i < n; ++i) {
        ver[1][i] = {p[i].i, n - p[i].j + 1};
    }

    for (int i = 0; i < n; ++i) {
        ver[2][i] = {n - p[i].i + 1, p[i].j};
    }

    for (int i = 0; i < n; ++i) {
        ver[3][i] = {n - p[i].i + 1, n - p[i].j + 1};
    }

    for (int i = 0; i < n; ++i) {
        ver[4][i] = {p[i].j, p[i].i};
    }
    for (int i = 0; i < n; ++i) {
        ver[5][i] = {p[i].j, n - p[i].i + 1};
    }

    for (int i = 0; i < n; ++i) {
        ver[6][i] = {n - p[i].j + 1, p[i].i};
    }

    for (int i = 0; i < n; ++i) {
        ver[7][i] = {n - p[i].j + 1, n - p[i].i + 1};
    }

    for (int i = 0; i < 8; ++i) {
        mem[i] = cnt (ver[i]);
    }

    ll nw = 0;

    for (int i = 0; i < q; ++i) {
        ll b;
        cin >> b;
        if (b == 1){
            if (nw == 0){
                nw = 1;
            }
            else if (nw == 1){
                nw = 0;
            }
            else if (nw == 2) {
                nw = 3;
            }
            else if (nw == 3){
                nw = 2;
            }
            else if (nw == 4){
                nw = 5;
            }
            else if (nw == 5){
                nw = 4;
            }
            else if (nw == 6){
                nw = 7;
            }
            else if (nw == 7){
                nw = 6;
            }
        }
        else if (b == 2) {
            if (nw == 0){
                nw = 2;
            }
            else if (nw == 1){
                nw = 3;
            }
            else if (nw == 2){
                nw = 0;
            }
            else if (nw == 3){
                nw = 1;
            }
            else if (nw == 4){
                nw = 6;
            }
            else if (nw == 5){
                nw = 7;
            }
            else if (nw == 6){
                nw = 4;
            }
            else if (nw == 7){
                nw = 5;
            }
        }
        else{
            if (nw == 0){
                nw = 4;
            }
            else if (nw == 1){
                nw = 6;
            }
            else if (nw == 2){
                nw = 5;
            }
            else if (nw == 3){
                nw = 7;
            }
            else if (nw == 4){
                nw = 0;
            }
            else if (nw == 5){
                nw = 2;
            }
            else if (nw == 6){
                nw = 1;
            }
            else if (nw == 7){
                nw = 3;
            }
        }
        cout << mem[nw] << ' ';
    }
}