#include <bits/stdc++.h>
using namespace std;


#define int long long
#define line cout << "\n"
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define sz(a) (int) a.size()
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pii pair<int, int>

typedef string str;
typedef long long ll;
typedef long double ld;
const int MOD = 998244353;


int bpow(int a, int b) {
    if (b == 0) {
        return 1;
    }
    if (b % 2) {
        return bpow(a, b - 1) * a % MOD;
    }
    int t = bpow(a, b / 2);
    return t * t % MOD;
}


int calc(vector<int> &a) {
    int res = 0;
    for (int i = 0; i < sz(a); i++) {
        res = (res + bpow(a[i], i + 1)) % MOD;
    }
    return res;
}


map<vector<int>, int> dp;
map<vector<int>, int> idx;
int ans[100];
int g[100][4];
int cur = 0;

void rec(vector<int> &a) {
    dp[a] = calc(a);
    int id = cur++;
    ans[id] = dp[a];
    idx[a] = id;
    // 1
    reverse(all(a));
    if (dp.find(a) == dp.end()) {
        rec(a);
    }
    g[id][1] = idx[a];
    reverse(all(a));

    // 2
    for (int i = 0; i < sz(a); i++) {
        a[i] = sz(a) - a[i] + 1;
    }
    if (dp.find(a) == dp.end()) {
        rec(a);
    }
    g[id][2] = idx[a];
    for (int i = 0; i < sz(a); i++) {
        a[i] = sz(a) - a[i] + 1;
    }

    // 3
    int n = sz(a);
    vector<int> t(n);
    for (int i = 0; i < n; i++) {
        t[a[i] - 1] = i + 1;
    }
    if (dp.find(t) == dp.end()) {
        rec(t);
    }
    g[id][3] = idx[t];
}

void solve() {
    int n, q;
    cin >> n >> q;
    vector<int> a(n);
    for (auto &u : a) {
        cin >> u;
    }
    rec(a);
    int v = 0;
    for (int i = 0; i < q; i++) {
        int x;
        cin >> x;
        v = g[v][x];
        cout << ans[v] << " ";
    }
}


signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);


#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
}