#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
using ll = long long;
using vl = __int128_t;
using ld = long double;
using ordered_set = tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>;
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
const ll mod = 998244353;
ll pw(ll a, ll b) {
    ll r = 1;
    while (b) {
        if (b & 1) r = (r * a) % mod;
        a = (a * a) % mod, b >>= 1;
    }
    return r;
}
void solve() {
    ll n, m;
    cin >> n >> m;
    vector<ll> p(n);
    for (ll& x : p) cin >> x;
    ll f1 = 0, f2 = 0, f3 = 0, j = -1;
    vector<ll> s1, s2, s3;
    while (m--) {
        j++;
        ll tp;
        cin >> tp;
        if (tp == 1) {
            s1.push_back(j);
            f1 ^= 1;
        } else if (tp == 2) {
            s2.push_back(j);
            f2 ^= 1;
        } else {
            s3.push_back(j);
            f3 ^= 1;
        }
        vector<array<ll, 3>> cr;
        if (s1.size()) cr.push_back({s1.back(), f1, 1});
        if (s2.size()) cr.push_back({s2.back(), f2, 2});
        if (s3.size()) cr.push_back({s3.back(), f3, 3});
        sort(all(cr));
        vector<ll> p2 = p;
        for (ll i = 0; i < cr.size(); i++) {
            if (cr[i][2] == 1 && cr[i][1]) {
                vector<ll> q(n);
                for (ll i = 0; i < n; i++) q[i] = p2[n - i - 1];
                p2 = q;
            } else if (cr[i][2] == 2 && cr[i][1]) {
                vector<ll> q(n);
                for (ll i = 0; i < n; i++) q[i] = n - p2[i] + 1;
                p2 = q;
            } else if (cr[i][2] == 3 && cr[i][1]) {
                vector<ll> q(n);
                for (ll i = 0; i < n; i++) q[p2[i] - 1] = i + 1;
                p2 = q;
            }
        }
        ll ans = 0;
        for (ll i = 0; i < n; i++) {
            ans += pw(p2[i], i + 1);
            if (ans >= mod) ans -= mod;
        }
        cout << ans << ' ';
    }
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
