#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
//#include <bits/extc++.h>
using namespace std;
//using namespace __gnu_pbds;
using ll = long long;
using ld = long double;
using pii = pair<ll, ll>;
using pic = pair<ll, char>;
using ull = unsigned long long;
//using oset = tree<ll, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rnd('S' + 'E' + 'E' + 'D');
ll l_r(const ll &lb_, const ll &rb_) {return (lb_ + rnd() % (rb_ - lb_ + 1)); }
template<class T> ll szl(const T &v1) { return (ll)v1.size(); }
template<class T> bool mmin(T &v1, const T &v2) { return (v1 > v2 ? v1 = v2, 1 : 0); }
template<class T> bool mmax(T &v1, const T &v2) { return (v1 < v2 ? v1 = v2, 1 : 0); }
#ifndef MiracleFFT
    #pragma GCC optimize("O3,unroll-loops")
    #pragma GCC target("avx,avx2,bmi,bmi2,lzcnt,popcnt")
#endif
const ll INF = 2e9 + 10;
const ll LNF = 2e18 + 10;
const ll MAXN = 2e5 + 10;
const ll MAXX = 1e9 + 10;
const ll MAXC = 1e18 + 10;
const ll ALPH = 26;
const ll BL_SZ = 400;
const ll MAX_LOG = 20;
const ll CNT_OP = 100;
const ll MOD = 998244353;
const ll P = 179;
const ll M = 1e9 + 787;
const ld UNO = ld(1.0);
const ld PI = atan2(0, -1);
const ld BURN = ld(0.99999);

ll pw(ll x, ll n) {
    if (n == 0) return 1;
    if (n % 2) return (x * pw(x, n - 1)) % MOD;
    ll a = pw(x, n / 2); return (a * a) % MOD;
}

void mke(vector<ll> &a, ll tp) {
    if (tp == 0) {
        reverse(a.begin(), a.end());
        return;
    }
    if (tp == 1) {
        for (auto &el : a) {
            el = szl(a) - 1 - el;
        }
        return;
    }

    vector<ll> id(szl(a));
    for (ll i = 0; i < szl(a); i++) {
        id[a[i]] = i;
    }
    a = id;
}
ll cost(const vector<ll> &a) {
    ll s = 0;
    for (ll i = 0; i < szl(a); i++) {
        s = (s + pw(a[i] + 1, i + 1)) % MOD;
    }
    return s;
}

void solve() {
    ll n, q;
    cin >> n >> q;
    vector<ll> a(n);
    for (auto &el : a) {
        cin >> el;
        --el;
    }

    map<ll, ll> f;
    vector<ll> c(3);
    while (q--) {
        ll t;
        cin >> t;
        --t;
        c[t]++;

        /*auto z = a;
        for (ll i = 0; i < 3; i++) {
            if (c[i] % 2) {
                mke(z, i);
            }
        }*/
        mke(a, t);
        cout << cost(a) << ' ';
        f[cost(a)]++;
    }
    if (szl(f) > 8) throw(0);
    cout << '\n';
}


int main() {
    bool IS_MULTITEST = 0; 
    #ifdef MiracleFFT
        IS_MULTITEST = 1;
        freopen("ain.txt", "r", stdin);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout << setprecision(9) << fixed;
    ll t = 1;
    if (IS_MULTITEST) {
        cin >> t;
    }
    while (t--) {
        solve();
    }
    #ifdef MiracleFFT
        cout << setprecision(2) << fixed;
        cout << "Time: " << ((ld)clock() / CLOCKS_PER_SEC) << " Sec" << endl;
    #endif
}
