#include <bits/stdc++.h>

// #pragma GCC optimize("O3")
// // #pragma GCC optimize("Ofast")
// // #pragma GCC target("unroll_loops")

#define int long long
using ll = long long;
using ld = long double;
using namespace std;

// #include <ext/pb_ds/assoc_container.hpp>

//  using namespace __gnu_pbds;
// using ordered_set = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;

const int INF = 1e18, MAXN = 1e5, mod = 998244353;
int mod1 = 1e9 + 7, mod2 = 1e9 + 9;

int binpow(int a, int b) {
    if (b == 0) {
        return 1;
    }
    int aa = binpow(a, b / 2);
    if (b % 2 == 0) {
        return (aa * aa) % mod;
    }
    return ((aa * aa) % mod * a) % mod;
}

void solve();

signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int tt = 1;
    //cin >> tt;
    while (tt--) {
        solve();
        cout << "\n";
    }
}

struct node {
    vector<int> p;
    int c;
};

map<vector<int>, int> mp;
map<pair<vector<int>, int>, vector<int> > mp1;

void solve() {
    int n, q;
    cin >> n >> q;
    vector<int> p(n);
    for (int i = 0; i < n; i++) {
        cin >> p[i];
    }
    deque<vector<int> > d;
    d.push_back(p);
    while (!d.empty()) {
        vector arr = d[0];
        d.pop_front();
        if (mp[arr] != 0) {
            continue;
        }
        if (mp[arr] == 0) {
            int sm = 0;
            for (int i = 0; i < n; i++) {
                sm += binpow(arr[i], i + 1);
                sm %= mod;
            }
            mp[arr] = sm;
        }
        vector<int> arr1 = arr;
        reverse(arr1.begin(), arr1.end());
        if (mp[arr1] == 0) {
            d.push_back(arr1);
        }
        mp1[{arr, 1}] = arr1;

        for (int i = 0; i < n; i++) {
            arr1[i] = (n + 1 - arr[i]);
        }
        if (mp[arr1] == 0) {
            d.push_back(arr1);
        }
        mp1[{arr, 2}] = arr1;

        arr1.assign(n, 0);
        for (int i = 0; i < n; i++) {
            arr1[arr[i] - 1] = i + 1;
        }
        if (mp[arr1] == 0)
            d.push_back(arr1);
        mp1[{arr, 3}] = arr1;
    }
    if (mp.size() > 20) {
        assert(0);
    }

    assert(0);

    while (q--) {
        int x;
        cin >> x;
        p = mp1[{p, x}];
        cout << mp[p] << ' ';
    }
}
