#include<bits/stdc++.h>
#define ll long long
#define ld long double

using namespace std;

ll n, q, MOD = 998244353;
vector<ll> p, b;

ll BVS(ll a, ll b) {
    if (b == 0) {
        return 1;
    } else if (b % 2 == 0) {
        return BVS((a * a) % MOD, b / 2);
    } else {
        return (BVS(a, b - 1) * a) % MOD;
    }
}

void input() {
    cin >> n >> q;
    p.resize(n);
    b.resize(q);
    for (ll i = 0; i < n; i++) {
        cin >> p[i];
    }
    for (ll i = 0; i < q; i++) {
        cin >> b[i];
    }
}

void fir() {
    vector<ll> ret(n);
    for (ll i = 0; i < n; i++) {
        ret[i] = p[n - i - 1];
    }
    p = ret;
}

void sec() {
    vector<ll> ret(n);
    for (ll i = 0; i < n; i++) {
        ret[i] = n - p[i] + 1;
    }
    p = ret;
}

void thir() {
    vector<ll> ret(n);
    for (ll i = 0; i < n; i++) {
        ret[p[i] - 1] = i + 1;
    }
    p = ret;
}

void solve() {
        ll sum00 = 0;
        for (ll i = 0; i < n; i++) {
            sum00 += BVS(p[i], i + 1);
            sum00 %= MOD;
        }
        fir();
        ll sum01 = 0;
        for (ll i = 0; i < n; i++) {
            sum01 += BVS(p[i], i + 1);
            sum01 %= MOD;
        }
        sec();
        ll sum11 = 0;
        for (ll i = 0; i < n; i++) {
            sum11 += BVS(p[i], i + 1);
            sum11 %= MOD;
        }
        fir();
        ll sum10 = 0;
        for (ll i = 0; i < n; i++) {
            sum10 += BVS(p[i], i + 1);
            sum10 %= MOD;
        }
        bool f = false, s = false;
    for (ll i = 0; i < q; i++) {
        if (b[i] == 1) {
            f = !f;
        } else {
            s = !s;
        }
        if (f == 0 && s == 0) {
            cout << sum00 << " ";
        } else if (f == 1 && s == 0) {
            cout << sum10 << " ";
        } else if (f == 0 && s == 1) {
            cout << sum01 << " ";
        } else if (f == 1 && s == 1) {
            cout << sum11 << " ";
        }
    }
}

void output() {

}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    input();
    solve();
    output();
    return 0;
}
