#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

using ll = long long;
#define int long long
const int MOD = 998244353;
const int INF = 1e18;

using ordered_set = tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>;

void solve1();

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int qwerty = 1;
    // cin >> qwerty;
    while (qwerty--) {
        solve1();
    }
}

int fast_pow(int x, int step) {
    if (step == 0) return 1;
    int half = fast_pow(x, step / 2);
    if (step % 2 == 0) {
        return half * half % MOD;
    }
    return half * half % MOD * x % MOD;
}

void solve1() {
    int n, m;
    cin >> n >> m;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<vector<vector<int> > > ans(2, vector<vector<int> >(2, vector<int>(2)));
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            for (int k = 0; k < 2; k++) {
                vector<int> q(n);
                if (k == 0) q = a;
                if (k == 1) {
                    for (int f = 0; f < n; f++) {
                        q[a[f] - 1] = f + 1;
                    }
                }
                if (i == 1) {
                    reverse(q.begin(), q.end());
                }
                if (j == 1) {
                    for (int f = 0; f < n; f++) {
                        q[f] = n - q[f] + 1;
                    }
                }
                int now = 0;
                for (int f = 0; f < n; f++) {
                    now += fast_pow(q[f], f + 1);
                    now %= MOD;
                }
                ans[k][i][j] = now;
            }
        }
    }
    int cn1 = 0, cn2 = 0, cn3 = 0;
    while (m--) {
        int f;
        cin >> f;
        if (f == 1) {
            cn1++;
        } else if (f == 2) {
            cn2++;
        } else {
            swap(cn1, cn2);
            cn3++;
        }
        cn1 %= 2;
        cn2 %= 2;
        cn3 %= 2;
        cout << ans[cn3][cn1][cn2] << ' ';
    }
}
