#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
using ld = long double;
using ll = long long;
using pii = pair<int,int>;
const int inf = 2e9;
const int M = 998244353;
const ld eps = 1e-9;
int n;

ll bin_pow(ll x, ll power) {
    if (power == 0) {
        return 1;
    }
    if (power == 1) {
        return x;
    }
    if (power == 2) {
        return x*x;
    }
    if (power % 2 == 0) {
        auto k = bin_pow(x, power/2);
        return (k*k)% M;
    }else {
        auto k = bin_pow(x, power-1);
        return (k*x)% M;
    }
}



ll sum(vector <ll> &a){
    ll sum = 0;
    for(int i = 1; i <=n;i++){
        sum+=bin_pow(a[i], i);
        if(sum >= M){
            sum%=M;
        }
    }
    return sum;
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    ll q;
    cin >> n >> q;
    vector<ll> a(n+1, 0), a1(n+1,0), a2(n+1, 0), a3(n+1, 0);
    for(int i =1; i <= n;i++){
        cin >> a[i];
    }
    for(int i = 1; i <= n;i++){
        a1[i] =  a[n-i+1];
    }
    for(int i = 1; i <= n;i++){
        a2[i] =  n-a[i]+1;
    }
    for(int i = 1; i <= n;i++){
        a3[a[i]] = i;
    }
    const ll sum0 = sum(a), sum1 = sum(a1), sum2 = sum(a2), sum3 = sum(a3);
    int cnt = 0;
    while (q--) {
        int z; cin >> z;
        if (cnt %2 == 0) {
            if (z == 1) {
                cout << sum1 << " ";
            }else if (z == 2) {
                cout << sum2 << " ";
            }else {
                cout << sum3 << " ";
                return 1;
            }
            cnt++;
        }else {
            cout << sum0 << " ";
            cnt++;
        }
    }
}
