#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(time(0));
constexpr int MAXN = 1e5 + 5;
constexpr int MOD = 998244353;
int pls(int a, int b){
    if (a + b >= MOD){
        return a + b - MOD;
    }
    return a + b;
}

int mult(int a, int b){
    return 1ll * a * b % MOD;
}

int bin_pow(int a, int n){
    if (n == 0){
        return 1;
    }
    int x = bin_pow(a, n / 2);
    if (n % 2 == 0){
        return mult(x, x);
    }
    else{
        return mult(mult(x, x), a);
    }
}
int p[MAXN];
int q[MAXN];
int d[MAXN];
int n, qq;
map <array <int, 3>, int> dct;
void func(int b1, int b2, int b3){
    for (int i = 0; i < b3; i++){
        for (int i = 1; i <= n; i++){
            d[q[i]] = i;
        }
        for (int i = 1; i <= n; i++){
            q[i] = d[i];
        }
    }
    for (int i = 0; i < b2; i++){
        for (int i = 1; i <= n; i++){
            q[i] = n - q[i] + 1;
        }
    }
    for (int i = 0; i < b1; i++){
        reverse(q + 1, q + n + 1);
    }
    int ans = 0;
    for (int i = 1; i <= n; i++){
        ans = pls(ans, bin_pow(q[i], i));
    }
    dct[{b1, b2, b3}] = ans;
}
void solve(){
    cin >> n >> qq;
    for (int i = 1; i <= n; i++){
        cin >> p[i];
    }
    int eps = 2;
    for (int b1 = 0; b1 < eps; b1++){
        for (int b2 = 0; b2 < eps; b2++){
            for (int b3 = 0; b3 < eps; b3++){
                for (int i = 1; i <= n; i++){
                    q[i] = p[i];
                }
                func(b1, b2, b3);
            }
        }
    }
    int cnt1 = 0;
    int cnt2 = 0;
    int cnt3 = 0;
    while (qq--){
        int b;
        cin >> b;
        if (b == 1){
            cnt1 += 1;
            if (cnt1 >= eps){
                cnt1 -= eps;
            }
        }
        else if (b == 2){
            cnt2 += 1;
            if (cnt2 >= eps){
                cnt2 -= eps;
            }
        }
        else{
            cnt3 += 1;
            if (cnt3 >= eps){
                cnt3 -= eps;
            }
        }
        cout << dct[{cnt1, cnt2, cnt3}] << ' ';
    }
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t = 1;
    while (t--){
        solve();
    }
}


