#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <limits>
#include <map>
#include <queue>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;

const int INF = 1e9 + 7;
const ll LL_INF = 4e18;

const ll MOD = 998244353;

vector<int> revd(vector<int> arr) {
    vector<int> ans = arr;
    reverse(ans.begin(), ans.end());

    return ans;
}

ll bin_pow(ll a, ll p) {
    if (p == 0) return 1;
    if (p & 1) return (a * bin_pow(a, p - 1)) % MOD;
    else {
        ll temp = bin_pow(a, p / 2);
        return (temp * temp) % MOD;
    }
}

vector<int> invd(vector<int> arr) {
    int n = arr.size();
    vector<int> ans(n);
    for (int i = 0; i < n; ++i) {
        ans[i] = n - arr[i] + 1;
    }

    return ans;
}

vector<int> get_obr(vector<int> arr) {
    int n = arr.size();
    vector<int> ans(n);
    for (int i = 0; i < n; ++i) {
        ans[arr[i] - 1] = i + 1;
    }

    return ans;
}

ll get_cost(const vector<int>& arr) {
    ll ans = 0;
    for (int i = 0; i < arr.size(); ++i) {
        ans += bin_pow(arr[i], i + 1);
        ans %= MOD;
    }
    return ans;
}

const int TO_CUT = 20;

vector<int> trim(const vector<int>& cur) {
    int n = cur.size();
    vector<int> ans(min(n, TO_CUT));
    for (int i = 0; i < min(n, TO_CUT); ++i) {
        ans[i] = cur[i];
    }

    return ans;
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    int n, nQ;
    cin >> n >> nQ;

    //vector<int> a = get_obr({4, 2, 5, 3, 1});
    // for (const int el : a) cout << el << ' ';
    // cout << '\n';

    map<vector<int>, bool> enc;
    vector<ll> costs;

    vector<int> perm(n);

    for (int i = 0; i < n; ++i) {
        cin >> perm[i];
    }
    vector<int> qs(nQ);
    for (int i = 0; i < nQ; ++i) cin >> qs[i];

    queue<vector<int>> q;
    q.push(perm);
    enc[perm] = true;

    vector<vector<int>> alls;

    while (!q.empty()) {
        vector<int> curp = q.front(); q.pop();

        vector<int> np = revd(curp);
        if (!enc.count(np)) {
            enc[np] = true;
            q.push(np);
        }
        np = invd(curp);
        if (!enc.count(np)) {
            enc[np] = true;
            q.push(np);
        }

        np = get_obr(curp);
        if (!enc.count(np)) {
            enc[np] = true;
            q.push(np);
        }
    }

    vector<vector<int>> adj(50, vector<int>(4));
    assert(enc.size() <= 50);
    for (const auto& [vec, fl] : enc) {
        alls.push_back(vec);
        costs.push_back(get_cost(vec));
    }

    int sz = alls.size();

    for (int i = 0; i < sz; ++i) {
        // for (const int el : alls[i]) cout << el << ' ';
        // cout << '\n';
        // cout << "AND COST " << costs[i] << '\n';

        vector<int> nw = revd(alls[i]);
        for (int to = 0; to < sz; ++to) {
            if (nw == alls[to]) adj[i][1] = to;
        }

        nw = invd(alls[i]);
        for (int to = 0; to < sz; ++to) {
            if (nw == alls[to]) adj[i][2] = to;
        }

        nw = get_obr(alls[i]);
        for (int to = 0; to < sz; ++to) {
            if (nw == alls[to]) adj[i][3] = to;
        }

        // cout << "TO " << adj[i][1] << ' ' << adj[i][2] << ' ' << adj[i][3] << '\n';
    }

    int now = 0;
    for (int i = 0; i < sz; ++i) {
        if (perm == alls[i]) {
            now = i;
            break;
        }
    }

    for (const int type : qs) {
        // cout << "GO FROM " << now << " TO " << adj[now][type] << '\n';
        now = adj[now][type];
        cout << costs[now] << ' ';
    }

    return 0;
}