#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

void fast_in_out() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << setprecision(18) << fixed;
#ifdef LOCAL
    freopen("input.txt", "r",stdin);
    freopen("output.txt", "w",stdout);
#endif
}

typedef tree<int, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
#define all(x) (x).begin(), (x).end()
#define int long long
using ll = long long;
const int N = 1e5, mod = 998244353;

int binpow(int a, int b) {
    if (b < 0) {
        b += mod - 1;
    }
    int res = 1;
    for (; b; b /= 2) {
        if (b & 1) {
            res = (ll) res * a % mod;
        }
        a = (ll) a * a % mod;
    }
    return res;
}

vector<vector<int> > a;
map<int, int> mp;
map<pair<int, int>, int> k;
int n, q;

void solve() {
    cin >> n >> q;
    a.resize(1, vector<int>(n));
    for (int i = 0; i < n; ++i) {
        cin >> a[0][i];
        --a[0][i];
    }
    for (int i = 0; i < (int) a.size(); ++i) {
        int ans = 0;
        for (int j = 0; j < n; ++j) {
            ans = (ans + binpow(a[i][j] + 1, j + 1)) % mod;
        }
        mp[i] = ans;
        if (true) {
            vector<int> b = a[i];
            reverse(all(b));
            int ok = -1;
            for (int i1 = 0; i1 < (int) a.size(); ++i1) {
                if (b == a[i1]) {
                    ok = i1;
                    break;
                }
            }
            if (ok == -1) {
                a.emplace_back(b);
                ok = (int) a.size() - 1;
            }
            k[{i, 1}] = ok;
        }
        if (true) {
            vector<int> b = a[i];
            for (auto &it: b) {
                it = n - 1 - it;
            }
            int ok = -1;
            for (int i1 = 0; i1 < (int) a.size(); ++i1) {
                if (b == a[i1]) {
                    ok = i1;
                    break;
                }
            }
            if (ok == -1) {
                a.emplace_back(b);
                ok = (int) a.size() - 1;
            }
            k[{i, 2}] = ok;
        }
        if (true) {
            vector<int> b = a[i];
            for (int j = 0; j < n; ++j) {
                b[a[i][j]] = j;
            }
            int ok = -1;
            for (int i1 = 0; i1 < (int) a.size(); ++i1) {
                if (b == a[i1]) {
                    ok = i1;
                    break;
                }
            }
            if (ok == -1) {
                a.emplace_back(b);
                ok = (int) a.size() - 1;
            }
            k[{i, 3}] = ok;
        }
    }
    int pos = 0;
    while (q--) {
        int h;
        cin >> h;
        pos = k[{pos, h}];
        cout << mp[pos] << ' ';
    }
}

signed main() {
    fast_in_out();
    int qwe = 1;
    // cin >> qwe;
    while (qwe--) {
        solve();
    }
}
