#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 % mod;
}


int M = 1e9 + 7;
int hesh(vector<int> &P) {
    int ans = 0;
    for (int i = 0; i < (int) P.size(); i++) {
        ans *= 100001;
        ans += P[i];
        ans %= M;
    }
    return ans % M;
}

struct YY {
    int hesh = -1;
    int ans = -1;
};

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

void rec(vector<int> pere) {
    int U = hesh(pere);

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

            reverse(pere.begin(), pere.end());
            int T1 = cost(pere) % mod;
            AA[{U, i}].ans = T1 % mod;
            AA[{U, i}].hesh = hesh(pere);
            rec(pere);
            reverse(pere.begin(), pere.end());
        }
        if (i == 2) {
            if (AA[{U, 2}].ans != -1) {
                continue;
            }
            vector<int> YY;
            for (int j = 0; j < n; j++) {
                YY.push_back((n - pere[j] + 1));
            }

            int T2 = cost(YY) % mod;
            AA[{U, i}].ans = T2 % mod;
            AA[{U, i}].hesh = hesh(YY);
            rec(YY);
        }

        if (i == 3) {
            if (AA[{U, 3}].ans != -1) {
                continue;
            }
            vector<int> Q(n);
            for (int j = 0; j < n; j++) {
                Q[pere[j] - 1] = j + 1;
            }

            int T3 = cost(Q) % mod;
            AA[{U, i}].ans = T3;
            AA[{U, i}].hesh = hesh(Q);;
            rec(Q);
        }
    }
}

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);

    int JJ = hesh(p);
    for (int i = 0; i < q; i++) {
        int a;
        cin >> a;
        P = AA[{JJ, a}].ans;
        JJ = AA[{JJ, a}].hesh;

        cout << P % mod << " ";
    }
    cout << endl;
}

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