#include <bits/stdc++.h>
#include <random>

#define i128 __int128
#define int long long
#define rint signed
#define map unordered_map

using namespace std;

random_device lal;
mt19937 rnd(lal());


const int inf = 1e18, mod = 998'244'353, base = 100003;

int binpow(int num, int power) {
    if (power == 0) { return 1; }
    if (power % 2 == 0) {
        int res = binpow(num, power / 2);
        return (res * res) % mod;
    }
    return (binpow(num, power - 1) * num) % mod;
}

int h(vector<rint> p) {
    int ans = 0;
    for (int i = 0; i < p.size(); ++i) {
        ans += ((int)p[i] * binpow(base, i + 1)) % mod;
        ans %= mod;
    }
    return ans;
}


vector<rint> do1(vector<rint> p) {
    for (int i = 0; i < p.size() / 2; ++i) {
        swap(p[i], p[p.size() - i - 1]);
    }
    return p;
}

vector<rint> do2(vector<rint> p) {
    for (int i = 0; i < p.size(); ++i) {
        p[i] = p.size() - p[i] + 1;
    }
    return p;
}

vector<rint> do3(vector<rint> p) {
    int n = p.size();
    vector<rint> p2(n);
    for (int i = 0; i < n; ++i) {
        p2[p[i] - 1] = i + 1;
    }
    return p2;
}

int calc(vector<rint> p) {
    int ans = 0;
    for (int i = 0; i < p.size(); ++i) {
        ans += binpow((int)p[i], i + 1);
        ans %= mod;
    }
    return ans;
}


signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, q;
    cin >> n >> q;
    vector<rint> p(n), b(q);
    for (rint &i: p) { cin >> i; }
    for (rint &i: b) { cin >> i; }

    map<int, vector<rint> > bi;
    map<int, vector<int> > g;
    map<int,int> ans;
    int cur = h(p);

    bi[cur] = p;
    ans[cur] = calc(bi[cur]);
    auto cur_p = bi[cur];
    bi[h(do1(cur_p))] = do1(cur_p);
    bi[h(do2(cur_p))] = do2(cur_p);
    bi[h(do3(cur_p))] = do3(cur_p);
    g[cur] = {
        h(do1(cur_p)),
        h(do2(cur_p)),
        h(do3(cur_p))
    };
    for (int i = 0; i < q; ++i) {
        cur = g[cur][b[i] - 1];
        if (ans.find(cur) == ans.end()) {
            ans[cur] = calc(bi[cur]);
            cur_p = bi[cur];
            bi[h(do1(cur_p))] = do1(cur_p);
            bi[h(do2(cur_p))] = do2(cur_p);
            bi[h(do3(cur_p))] = do3(cur_p);
            g[cur] = {
                h(do1(cur_p)),
                h(do2(cur_p)),
                h(do3(cur_p))
            };
        }
        cout << ans[cur] << ' ';
    }
}
