#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("avx,avx2,bmi2,fma")
// mt19937 rnd(24523);

const int MOD = 998244353, HMOD = 974137;

int binpow(int x, int k) {
    int ans = 1;
    while (k) {
        if (k % 2 == 0) {
            x = (x * x) % MOD;
            k /= 2;
        } else {
            ans = (ans * x) % MOD;
            k--;
        }
    }
    return ans;
}

vector<int> p(1e6, 1);
int get_hash(vector<int>& arr) {
    int ans = 0;
    for (int i = 0; i < (int)arr.size(); ++i) {
        ans = ((ans + (arr[i] * p[i]) % HMOD) % HMOD + HMOD) % HMOD;
    }
    return ans;
}

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);

    for (int i = 1; i < 1e6; ++i) {
        p[i] = (p[i - 1] * 179) % HMOD;
    }
    int n, q;
    cin >> n >> q;
    vector<int> inp(n);
    for (auto& it : inp) {
        cin >> it;
    }
    // int ans = 0;
    // for (int i = 0; i < n; ++i) {
    //     ans += binpow(inp[i], i + 1);
    //     ans %= MOD;
    // }
    // cout << ans << endl;

    vector<array<int, 3>> g(1e6, {-1, -1, -1});
    vector<vector<int>> inf(1e6);
    vector<int> ans(1e6, INT_MAX);
    int now = get_hash(inp);
    inf[now] = inp;

    while (q--) {
        int a;
        cin >> a;
        if (g[now][0] == -1) {
            vector<int> ex = inf[now];
            reverse(ex.begin(), ex.end());
            g[now][0] = get_hash(ex);
            inf[g[now][0]] = ex;

            ex = inf[now];
            for (int j = 0; j < n; ++j) {
                ex[j] = n - ex[j] + 1;
            }
            g[now][1] = get_hash(ex);
            inf[g[now][1]] = ex;

            for (int j = 0; j < n; ++j) {
                ex[inf[now][j] - 1] = j + 1;
            }
            g[now][2] = get_hash(ex);
            inf[g[now][2]] = ex;
        }
        now = g[now][a - 1];
        if (ans[now] == INT_MAX) {
            ans[now] = 0;
            for (int i = 0; i < n; ++i) {
                ans[now] += binpow(inf[now][i], i + 1);
                ans[now] %= MOD;
            }
        }
        cout << ans[now] << ' ';
    }
}

/*

158424
419232
2503846
406112


1092386

5 5
1 2 3 4 5
1 2 3 1 2

65 3413 3413 65 3413


5 6
5 3 1 4 2
3 3 1 2 3 1

293 303 3225 215 317 3209
*/