#include <bits/stdc++.h>

using namespace std;
#define int long long

map<vector<int>, int> number;

vector<int> t1(vector<int> p) {
    for (int i = 0; i < (int) p.size() - i - 1; i++)
        swap(p[i], p[(int) p.size() - i - 1]);
    return p;
}

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

vector<int> t3(vector<int> p) {
    vector<int> que((int) p.size());
    for (int i = 0; i < (int) p.size(); i++)
        que[p[i] - 1] = i + 1;
    return que;
}

map<int, vector<int> > go;

const int mod = 998244353;

int binpow(int x, int n) {
    if (n == 0) return 1;
    if (n % 2 == 0)
        return binpow((x * x) % mod, n / 2);
    return (x * binpow((x * x) % mod, n / 2)) % mod;
}

map<int, int> ans;

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, q, cur = 1;
    cin >> n >> q;
    vector<int> p(n);
    for (int i = 0; i < n; i++)
        cin >> p[i];
    number[p] = cur++;
    queue<vector<int>> que;
    que.push(p);
    while (!que.empty()) {
        p = que.front(), que.pop();
        int x = number[p];
        go[x].resize(3, -1);
        vector<int> g1 = t1(p), g2 = t2(p), g3 = t3(p);
        if (number[g1] == 0) {
            go[x][0] = cur;
            number[g1] = cur++, que.push(g1);
        } else go[x][0] = number[g1];
        if (number[g2] == 0) {
            go[x][1] = cur;
            number[g2] = cur++, que.push(g2);
        } else go[x][1] = number[g2];
        if (number[g3] == 0) {
            go[x][2] = cur;
            number[g3] = cur++, que.push(g3);
        } else go[x][2] = number[g3];
    }
    for (auto [p, x]: number) {
        int sum = 0;
        for (int i = 0; i < (int) p.size(); i++)
            sum = (sum + binpow(p[i], i + 1)) % mod;
        ans[x] = sum;
    }
    cur = 1;
    for (int i = 0; i < q; i++) {
        int b;
        cin >> b;
        cur = go[cur][b - 1];
        cout << ans[cur] << " ";
    }
    return 0;
}
