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

using namespace std;


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)%mod;
        return (Y * Y) % mod;
    }
    return (b * (binpow(a, b, p - 1))%mod) % 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;
}

struct YY {
    int OO = -1;
};

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

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

    for (int i = 1; i <= 3; i++) {
        int T1;
        int T2;
        int T3;
        if (i == 1) {
            if (AA[{U, 1}].OO != -1) {
                continue;
            }

            reverse(pere.begin(), pere.end());
            T1 = cost(pere);
            AA[{U, i}].OO = T1;
            rec(pere, op + 1);
            reverse(pere.begin(), pere.end());
        }
        if (i == 2) {
            if (AA[{U, 2}].OO != -1) {
                continue;
            }
            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}].OO = T2;
        }

        if (i == 3) {
            if (AA[{U, 3}].OO != -1) {
                continue;
            }
            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}].OO = 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);
    }

    int P = cost(p)%mod;
    int UUU = 0;

    rec(p, UUU);

    for (int i = 0; i < q; i++) {
        int a;
        cin >> a;
        P = AA[{P, a}].OO;
        cout << P%mod << " ";
    }
    cout << endl;
}

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