#include<bits/stdc++.h>
#define all(p) p.begin(),p.end()
using namespace std;
#define int long long
int mod = 998244353;
int binpow(int a, int n) {
    if (n == 0) {
        return 1;
    } else if (n & 1) {
        return binpow(a, n - 1) * a % mod;
    } else {
        return binpow(a * a % mod, n / 2);
    }
}
set<vector<int>>was;
map<vector<int>, int>nums;
vector<int>g[100];
int ans[100];
int sz = 0;
int n;
void dfs(int cur, vector<int>p) {
    ans[cur] = 0;
    for (int i = 1; i <= n; i++) {
        ans[cur] += binpow(p[i], i);
        ans[cur] %= mod;
    }
    was.insert(p);
    {
        vector<int>to = p;
        for (int i = 1; i <= n; i++) {
            to[i] = p[n - i + 1];
        }
        if (was.find(to) == was.end()) {
            nums[to] = sz;
            g[cur].push_back(sz);
            sz++;
            dfs(sz - 1, to);
        } else {
            g[cur].push_back(nums[to]);
        }
    }
    {
        vector<int>to = p;
        for (int i = 1; i <= n; i++) {
            to[i] = n - p[i] + 1;
        }
        if (was.find(to) == was.end()) {
            nums[to] = sz;
            g[cur].push_back(sz);
            sz++;
            dfs(sz - 1, to);
        } else {
            g[cur].push_back(nums[to]);
        }
    }
    {
        vector<int>to = p;
        for (int i = 1; i <= n; i++) {
            to[p[i]] = i;
        }
        if (was.find(to) == was.end()) {
            nums[to] = sz;
            g[cur].push_back(sz);
            sz++;
            dfs(sz - 1, to);
        } else {
            g[cur].push_back(nums[to]);
        }
    }
}
signed main() {
    cin >> n;
    int q;
    cin >> q;
    vector<int>p(n + 1);
    for (int i = 1; i <= n; i++) {
        cin >> p[i];
    }
    nums[p] = 0;
    sz++;
    dfs(0, p);
    int cur = 0;
    while (q--) {
        int t;
        cin >> t;
        t--;
        cur = g[cur][t];
        cout << ans[cur] << ' ';
    }

}