#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define all(x) x.begin(), x.end()
using ll = long long;
using ui = unsigned int;
using ull = unsigned long long;
using ld = long double;
template<typename T> 
using orset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const ll mod = 998244353;

int ans[2][2][2];

ll stmod(ll a, ll st) {
    if (st == 0) return 1;
    if (st & 1) return stmod(a, st - 1) * a % mod;
    int hf = stmod(a, st / 2);
    return (ll)hf * hf % mod;
}

int main () {
    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll n, q;
    cin >> n >> q;
    vector<ll> p(n);
    for (int i = 0; i < n; i++) {
        cin >> p[i];
        p[i]--;
    }
    for (int a = 0; a < 2; a++) {
        for (int b = 0; b < 2; b++) {
            for (int c = 0; c < 2; c++) {
                for (int i = 0; i < n; i++) {
                    ll jk = p[i];
                    if (a) jk = p[n - i - 1];
                    if (b) jk = n - jk - 1;
                    ans[a][b][c] += (c? stmod(i + 1, jk + 1) : stmod(jk + 1, i + 1));
                    ans[a][b][c] -= (ans[a][b][c] >= mod? mod : 0);
                    // cout << (c? jk + 1 : i + 1) << " " << (c? i + 1 : jk + 1) << "\n";
                }
                // cout << "\n";
            }
        }
    }
    int a = 0, b = 0, c = 0;
    for (int i = 0; i < q; i++) {
        int tp;
        cin >> tp;
        if (tp == 1) a = 1 - a;
        if (tp == 2) b = 1 - b;
        if (tp == 3) c = 1 - c;
        cout << ans[a][b][c] << " ";
    }
}
