#include<iostream>
#include<sstream>
#include<fstream>
#include<iomanip>
#include<vector>
#include<cmath>
#include<algorithm>
#include<climits>
#include<vector>
#include<set>
#include<map>
#include<deque>
#include <unordered_map>
#include<unordered_set>
#include<unordered_set>

#define int long long
using namespace std;

const int MOD = 998244353;

int power(int v, int p) {
    int res = 1;
    while (p>0) {
        if (p%2) {
            res = (res*v)%MOD;
        }
        p/=2;
        v = (v*v)%MOD;
    }
    return res;
}

int cost(vector<int>& p) {
    int c = 0;
    for (int i = 1; i<p.size(); ++i) {
        c = (c + power(p[i], i))%MOD;
    }
    return c;
}

int rev(vector<int>& p) {
    reverse(p.begin()+1, p.end());
    return cost(p);
}

int mirror(vector<int>& p) {
    for (int i = 1; i<p.size(); ++i) {
        p[i] = p.size()-p[i];
    }
    return cost(p);
}

int opposite(vector<int>& p) {
    vector<int> q(p.size());
    for (int i = 1; i<p.size(); ++i) {
        q[p[i]] = i;
    }
    p = q;
    return cost(p);
}

int modify(vector<int>& p, int t) {
    if (t==1) return rev(p);
    if (t==2) return mirror(p);
    return opposite(p);
}

void solve() {
    int n, q; cin >> n >> q;
    vector<int> p(n+1), res(q), b(q);
    unordered_set<int> bst;
    for (int i = 1; i<=n; ++i) cin >> p[i];
    for (int i = 0; i<q; ++i) {
        cin >> b[i];
        bst.insert(b[i]);
    }
    if (n*q<=1e6) {
        for (int i = 0; i<q; ++i) {
            res[i] = modify(p, b[i]);
        }
    }
    else if (bst.size()==1) {
        int c1 = cost(p);
        int c2 = modify(p, b[0]);
        for (int i = 0; i<q; ++i) {
            res[i] = (i%2)? c1 : c2;
        }
    }

    for (int r : res) cout << r << " ";
}


signed main() {
    cin.tie(nullptr);
    cout.tie(nullptr);
    ios_base::sync_with_stdio(false);

    solve();
    return 0;
}