#include <bits/stdc++.h>

using namespace std;

const int MOD = 998244353;

#define int long long

int powmod(int v, int mod) {
    int cr = 1;
    while (mod) {
        if (mod & 1) cr *= v;
        v *= v;
        cr %= MOD; v %= MOD; mod >>= 1;
    }
    return cr % MOD;
}

int32_t main() {
    int n; cin >> n; int m; cin >> m;
    vector<int> a(n); for (auto &i : a) cin >> i;

    int rs = 0, rsr = 0;
    for (int i = 0; i < n; i++) rs += powmod(a[i], i + 1);
    for (int i = 0; i < n; i++) rsr += powmod(a[n - i - 1], i + 1); rs %= MOD; rsr %= MOD;
    int am = 0;
    int change[100][3];
    int vl[1001];
    map<vector<int>, int> got; got[a] = am++;
    auto fs = [&](vector<int> &b) {
        auto c = b;
        for (int i = 0; i < n; i++) b[i] = c[n - 1 - i];
    };
    auto sc = [&](vector<int> &b) {
        for (int i = 0; i < n; i++) b[i] = n + 1 - b[i];
    };
    auto h = [&](vector<int> &b) {
        auto c = b;
        for (int i = 0; i < n; i++) c[b[i] - 1] = i + 1;
        b = c;
    };
    int got1[8];
    auto f = [&](auto &&f, int prev, int po, vector<int> carr) -> void {
        int cur = am;
        if (got.find(carr) != got.end()) { cur = got[carr]; } else cur = am++;
        change[prev][po] = cur;
        if (!got1[cur]) {
            for (int i = 0; i < n; i++) vl[cur] += powmod(carr[i], i + 1); vl[cur] %= MOD;
            change[prev][po] = cur;
            got[carr] = cur; fs(carr); f(f, cur, 0, carr); fs(carr); sc(carr); f(f, cur, 1, carr); sc(carr); h(carr); f(f, cur, 2, carr); h(carr);
        }
    };
    for (int j = 0; j < n; j++) vl[0] += powmod(a[j], j + 1); vl[0] %= MOD;
    for (int i = 0; i < n; i++) {
        fs(a); f(f, 0, 0, a); fs(a);
        sc(a); f(f, 0, 1, a); sc(a);
        h(a); f(f, 0, 2, a); h(a);
    }
    int cs = 0;
    for (int i = 0; i < m; i++) {
        int v; cin >> v; v--; cs = change[cs][v];
        cout << vl[cs] << ' ';
    }

    return 0;
}