#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define int long long
#define db double
#define ll long long
#define pii pair<int, int>
#define tiii tuple<int ,int, int >
#define tiiii tuple<int, int, int, int>
#define tiiiii tuple<int, int, int, int, int>
#define ve vector
#define vi ve<int>
#define vvi ve<vi>
#define all(a) a.begin(), a.end()
#define allr(a) a.rbegin(), a.rend()
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
#define cin(a) for(auto &now : a) cin >> now;
#define cin1(a) for(int i = 1; i < a.size() ++i) cin >> a[i];
#define print(a, s) for(auto now : a) cout << now << s;
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rnd(time(0));
const int M = 998244353;
int st(int a, int n) {
    if (n == 0) return 1;
    return (n % 2 == 0 ? st(a * a % M, n / 2) : a * st(a, n - 1) % M);
}
int n, q;
vi op0(vi p) {
    reverse(all(p));
    return p;
}
vi op1(vi p) {
    for (auto &now : p) {
        now = n + 1 - now;
    }
    return p;
}
int get_val(const vi& p) {
    int res = 0;
    for (int i= 0; i < n; ++i) {
        res += st(p[i], i + 1); res %= M;
    }
    return res;
}
vi op2(vi p) {
    vi q(n);
    for (int i = 0; i < n; ++i) {
        q[p[i] - 1] = i + 1;
    }
    return q;
}
void solve() {
    cin >> n >> q;
    vi p(n); cin(p);
    vvi v;
    for (int cnt = 0; cnt <= 5; ++cnt) {
        for (int mask = 0; mask < pow(3, cnt); ++mask) {
            v.push_back(p);
            for (int i  = 0; i < cnt; ++i) {
                int b = (int)(mask / pow(3, i)) % 3;
                if (b == 0) v.back() = op0(v.back());
                else if (b == 1) v.back() = op1(v.back());
                else v.back() = op2(v.back());
            }
        }
    }

    sort(all(v));
    v.resize(unique(all(v)) - v.begin());
    vvi mt(v.size(), vi(3, -1));
    vi val(v.size());
    for (int i = 0; i < v.size(); ++i) {
        val[i] = get_val(v[i]);
    }
    int tk = lower_bound(all(v), p) -v.begin();
    vi qs(q); cin(qs);
    for (auto b : qs) {
        --b;
        if (mt[tk][b] == -1) {
            vi q(n);
            if (b == 0) q = op0(v[tk]);
            else if (b == 1) q = op1(v[tk]);
            else q = op2(v[tk]);
            mt[tk][b] = lower_bound(all(v), q) - v.begin();
        }
        tk = mt[tk][b];
        cout << val[tk] << " ";
    }

}
int32_t main() {
    // ios::sync_with_stdio(0);
    // cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(9);

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