#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
using cll = const long long;
using ull = unsigned long long;

void Kus() { exit(0); }
ll rnd(cll RMOD) { return (rand() * rand()) % RMOD; };

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

template<typename a>
using pbds_tree = __gnu_pbds::tree<a, __gnu_pbds::null_type, less/**_equal*/<a>, 
__gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;

template<typename a>
struct oset : public pbds_tree<a> {
    auto operator [] (ll index) {
        return *this -> find_by_order(index);
    }
};

#define INTERACTIVE false

/** fun begins */

cll MOD = 998244353;

// ll fen[1000000];

// ll fen_sum(ll idx) {
//     ll sum = 0;
//     for (ll i = idx; i > 0; i -= i & -i) {
//         sum += fen[i];
//     }
//     return sum;
// }

// void fen_upd(ll idx, ll x) {
//     for (ll i = idx; i <= n; i += i & -i) {
//         fen[i] += x;
//     }
// }

// ll fen_get(ll tl, ll tr) {
//     return fen_sum(tr) - fen_sum(tl - 1);
// }
ll ans[2][2][2];

void rawr() {
    ll n, m;
    cin >> n >> m;
    vector <ll> a(n), b(n), c(n), d(n), a1(n), b1(n), c1(n), d1(n);
    for (ll i = 0; i < n; i++) {
        cin >> a[i];
        b[i] = n - a[i] + 1;
        c[n - i - 1] = a[i];
        d[n - i - 1] = b[i];
    }
    for (ll i = 0; i < n; i++) {
        ans[0][0][0] = (ans[0][0][0] + (ll)(pow(a[i], (i + 1))) % MOD) % MOD;
        ans[0][1][0] = (ans[0][1][0] + (ll)(pow(b[i], (i + 1))) % MOD) % MOD;
        ans[1][0][0] = (ans[1][0][0] + (ll)(pow(c[i], (i + 1))) % MOD) % MOD;
        ans[1][1][0] = (ans[1][1][0] + (ll)(pow(d[i], (i + 1))) % MOD) % MOD;
    }
    for (ll i = 0; i < n; i++) {
        a1[a[i] - 1] = i + 1;
        b1[b[i] - 1] = i + 1;
        c1[c[i] - 1] = i + 1;
        d1[d[i] - 1] = i + 1;
        
    }
    for (ll i = 0; i < n; i++) {
        ans[0][0][1] = (ans[0][0][1] + (ll)(pow(a1[i], (i + 1))) % MOD) % MOD;
        ans[0][1][1] = (ans[0][1][1] + (ll)(pow(b1[i], (i + 1))) % MOD) % MOD;
        ans[1][0][1] = (ans[1][0][1] + (ll)(pow(c1[i], (i + 1))) % MOD) % MOD;
        ans[1][1][1] = (ans[1][1][1] + (ll)(pow(d1[i], (i + 1))) % MOD) % MOD;
    }
    ll cnt1 = 0, cnt2 = 0, cnt3 = 0;
    for (ll i = 0; i < m; i++) {
        ll x;
        cin >> x;
        if (x == 1) {
            if (cnt3){
                cnt2++;
                cnt2 %= 2;
            } else {
                cnt1++;
                cnt1 %= 2;
            }
        } else if (x == 2) {
            if (cnt3){
                cnt1++;
                cnt1 %= 2;
            } else {
                cnt2++;
                cnt2 %= 2;
            }
        } else {
            cnt3++;
            swap(cnt1, cnt2);
            cnt3 %= 2;
        }
        cout << (ans[cnt1][cnt2][cnt3] + MOD) % MOD << " ";
    }
}

/** fun is over */

int main() {
    srand(time(0));
    random_device rd;
    mt19937 gen(rd());
    #if defined (LOCAL) && (INTERACTIVE == false)
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    /** Meow begins */
    ll _ = 1;
    // cin >> _;
    while (_--) {
        rawr();
    }
    /** Meow is over */
    Kus();
}
