#include <bits/stdc++.h>
#define int long long

using namespace std;

int inf = 1e9 + 7;
int mod = 998244353;

int binpow(int a,int b,int p) {
    if (p == 0) {
        return a;
    }
    if (p % 2 == 0) {
        int Y = binpow(a, b, p / 2);
        return (Y * Y) % mod;
    }
    return (b * binpow(a, b, p - 1)) % mod;
}

int cost(vector<int> &a) {
    int ans = 0;
    for (int i = 0; i < (int) a.size(); i++) {
        int T = 1;
        ans += binpow(T, a[i], (i + 1)) % mod;
        ans %= mod;
    }
    return ans;
}

map<pair<int,int>,int> AA;
int n;

void rec(vector<int> pere,int op) {
    if (op == 4) {
        return;
    }
    int U = cost(pere);

    for (int i = 1; i <= 3; i++) {
        int T1;
        int T2;
        int T3;
        if (i == 1) {
            reverse(pere.begin(), pere.end());
            T1 = cost(pere);
            AA[{U, i}] = T1;
            rec(pere, op + 1);
            reverse(pere.begin(), pere.end());
        }
        if (i == 2) {
            vector<int> YY;
            for (int j = 0; j < n; j++) {
                YY.push_back((n - pere[j] + 1));
            }
            rec(YY, op + 1);
            T2 = cost(YY);
            AA[{U, i}] = T2;
        }
        if (i == 3) {
            vector<int> Q(n);
            for (int j = 0; j < n; j++) {
                Q[pere[j] - 1] = j + 1;
            }
            rec(Q, op + 1);
            T3 = cost(Q);
            AA[{U, i}] = T3;
        }
    }
}

void solve() {
    int q;
    cin >> n >> q;
    vector<int> p;
    for (int i = 0; i < n; i++) {
        int a;
        cin >> a;
        p.push_back(a);
    }
    map<pair<int,int>,int> A;
    int P = cost(p);
    int UUU = 0;

    rec(p, UUU);
    for (int i = 0; i < q; i++) {
        int a;
        cin >> a;
        P = AA[{P, a}];
        cout << P << " ";
    }
    cout << endl;
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    while (t--) {
        solve();
    }
    return 0;
}
