// #define _GLIBCXX_DEBUG
// #pragma GCC optimize("O3,unroll-loops")
#include<iostream>
#include<vector>
using namespace std;
using ll = long long;
using ld = long double;
ll MOD = 998244353;
ll binpow(ll x, ll p){
    if (p == 0){
        return 1;
    }
    if (p&1){
        return (x*binpow(x, p-1))%MOD;
    }
    ll a = binpow(x, p/2);
    return (a*a)%MOD;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    ll n, q;
    cin>>n>>q;
    vector<ll> v(n), v1(n);
    for (auto&e:v){cin>>e;}
    while (q--){
        ll b;
        cin>>b;
        if (b == 1){
            for (ll i = 0; i<n; i++){
                v1[i] = v[n-i-1];
            }
        }
        if (b == 2){
            for (ll i = 0; i<n; i++){
                v1[i] = n - v[i] +1;
            }
        }
        if (b == 3){
            for (ll i = 0; i<n; i++){
                v1[v[i]-1] = i+1;
            }
        }
        ll ans = 0;
        swap(v1, v);
        for (ll i = 0; i<n; i++){
            ans = (ans + binpow(v[i], i+1))%MOD;
        }
        cout<<ans<<' ';
    }
    return 0;
}