#include <bits/stdc++.h>

using namespace std;

long long MOD = 998244353;

long long binary_pow(long long x, long long y){
    if (y == 0){
        return 1;
    }

    long long tmp = binary_pow(x, y/2);

    if (y % 2 == 0){
        return tmp*tmp%MOD;
    } else {
        return tmp*tmp%MOD*x%MOD;
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n, q;

    cin >> n >> q;

    vector<long long> lst_els(n+1, 0);

    for (int i = 1; i <= n; ++i){
        cin >> lst_els[i];
    }
    
    for (int i = 0; i < q; ++i){
        int type;
        cin >> type;
        vector<long long> new_lst(n+1, 0);

        for (int j = 1; j <= n; ++j){
            if (type == 1){
                new_lst[j] = lst_els[n-j+1];
            } else if (type == 2){
                new_lst[j] = n-lst_els[j]+1;
            } else {
                new_lst[lst_els[j]] = j;
            }
        }

        long long ans = 0;

        for (int j = 1; j <= n; ++j){
            ans = (ans+binary_pow(new_lst[j], j))%MOD;
        }

        cout << ans << " ";
        swap(lst_els, new_lst);
        new_lst.clear();
    }

}