#include <bits/stdc++.h>

using namespace std;

#ifndef lisie_bimbi
#define endl '\n'
#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2,bmi2,fma")
#endif

using ll = long long;

// #define int long long

const int mod = 998'244'353;

int add(int a, int b){
    if(a + b < mod){
        return a + b;
    }else{
        return a + b - mod;
    }
}

int mul(int a, int b){
    return ((ll)a * b) % mod;
}

int binpow(int a, int b){
    if(b == 0){
        return 1;
    }
    int ans = binpow(a, b / 2);
    ans = mul(ans, ans);
    if(b % 2 == 1){
        ans = mul(ans, a);
    }
    return ans;
}


void solve(){
    int n, q;
    cin >> n >> q;
    vector<int> a(n);
    vector<vector<int>> v(8, vector<int>(3));
    vector<int> ans(8);
    v[0] = {2, 1, 4};
    v[1] = {3, 0, 6};
    v[2] = {0, 3, 5};
    v[3] = {1, 2, 7};
    v[4] = {6, 5, 0};
    v[5] = {7, 4, 2};
    v[6] = {4, 7, 1};
    v[7] = {5, 6, 3};
    for(int j = 0; j < n; j++){
        cin >> a[j];
        int i = j + 1;
        int p = a[j];
        ans[0] = add(ans[0], binpow(p, i));
        ans[1] = add(ans[1], binpow(n + 1 - p, i));
        ans[2] = add(ans[2], binpow(p, n + 1 - i));
        ans[3] = add(ans[3], binpow(n + 1 - p, n + 1 - i));
        ans[4] = add(ans[4], binpow(i, p));
        ans[5] = add(ans[5], binpow(n + 1 - i, p));
        ans[6] = add(ans[6], binpow(i, n + 1 - p));
        ans[7] = add(ans[7], binpow(n + 1 - i, n + 1 - p));
    }
    int now = 0;
    for(int i = 0; i < q; i++){
        int b;
        cin >> b;
        b--;
        now = v[now][b];
        cout << ans[now] << ' ';
    }
    cout << endl;
}

signed main(){
#ifdef lisie_bimbi
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);   
#else
    cin.tie(nullptr)->sync_with_stdio(false);
#endif         
    int t = 1;
    //cin >> t;
    while(t--){
        solve();
    }
}