#include <bits/stdc++.h>
#define ll long long
#define int ll
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define uniq(x) x.erase(unique(all(x)), x.end())
#define fast_cin() cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0)

#pragma optimize("O3")
#pragma target("avx2")
using namespace std;


int n;
int mod =  998244353;

int p2(int x, int p) {
    if (p == 0) return 1;
    int res = p2(x, p/2);
    if (p&1) return res*res%mod*x%mod;
    return res*res%mod;
}

vector<int> op1(vector<int> a) {
    reverse(all(a));
    return a;
}

vector<int> op2(vector<int> a) {
    for (int i = 0; i < n; ++i) {
        a[i] = n-a[i]+1;
    }
    return a;
}

vector<int> op3(vector<int> a) {
    vector<int> p(n);
    for (int i = 0; i < n; ++i) {
        p[a[i]-1] = i+1;
    }
    return p;
}

int fans(vector<int> &a) {
    int ns = 0;
    for (int i = 0; i < n; ++i) {
        ns = (ns+p2(a[i], i+1))%mod;
    }
    return ns;
}

vector<vector<int>> pon;

int ff(vector<int> a) {
    for (int i = 0; i < pon.size(); ++i) {
        if (pon[i] == a) return i;
    }
    return -1;
}

signed main() {
    fast_cin();
    int q; cin >> n >> q;
    vector<int> a(n);
    for (int &i: a) cin >> i;
    set<vector<int>> b;
    b.insert(a);
    for (int i = 0; i < 3; ++i) {
        for (auto i: b) {
            b.insert(op1(i));
            b.insert(op2(i));
            b.insert(op3(i));
        }
    }

    for (auto i: b) pon.push_back(i);
    
    vector<int> ans;
    for (auto &i: pon) {
        ans.push_back(fans(i));
    }
    vector<vector<int>> per(pon.size(), vector<int> (3));

    for (int i = 0; i < pon.size(); ++i) {
        per[i][0] = ff(op1(pon[i]));
        per[i][1] = ff(op2(pon[i]));
        per[i][2] = ff(op3(pon[i]));
    }

    int now = ff(a);
    for (int i = 0; i < q; ++i) {
        int b; cin >> b;
        now = per[now][b-1];
        cout << ans[now] << ' ';
    }
}
