#ifndef LOCAL
#pragma GCC Optimize("O3, Ofast")
#pragma GCC Target("avx,avx2,unroll-loops")
#endif
#include <bits/stdc++.h>

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

#define int ll
#define f first
#define s second
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define pii pair<int, int>
#define pb push_back
#ifndef LOCAL
#define endl "\n"
#endif

mt19937 rnd(time(0));

const int MOD = 998244353;

int power(int x, int n) {
    int ans = 1;
    while (n) {
        if (n&1) {
            ans = (ans * x) % MOD;
        }
        n /= 2;
        x = (x * x) % MOD;
    }
    return ans;
}

void solve() {
    int n, q;
    cin >> n >> q;
    vector<int> p(n);
    for (auto &x : p) {
        cin >> x;
        --x;
    }
    map<vector<int>, int> M;
    vector<int> ans;
    vector<vector<int>> g;
    auto dfs = [&](vector<int> p, auto &&self) -> void {
        if (M.count(p)) {
            return;
        }
        M[p] = g.size();
        g.pb({-1, -1, -1});
        ans.pb(0);
        for (int i = 0; i < p.size(); ++i) {
            ans.back() += power(p[i] + 1, i + 1);
            ans.back() %= MOD;
        }
        vector<int> q = p;
        reverse(all(q));
        self(q, self);
        g[M[p]][0] = M[q];
        q = p;
        for (auto &x : q) {
            x = n - x - 1;
        }
        self(q, self);
        g[M[p]][1] = M[q];
        q = p;
        for (int i = 0; i < n; ++i) {
            q[p[i]] = i;
        }
        self(q, self);
        g[M[p]][2] = M[q];        
    };
    dfs(p, dfs);
    int now = 0;
    while (q--) {
        int b;
        cin >> b;
        --b;
        now = g[now][b];
        cout << ans[now] << endl;
    }
}

signed main() {
    #ifdef LOCAL
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
    #else
        ios::sync_with_stdio(false);
        cin.tie(0);
    #endif
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
}