#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define print(x) f{or (auto &_: x) {cout <<_<<" ";}; cout<<"\n";

int mod = 998244353;

int bpow(int a,int n) {
    if (n <= 0) {
        return 1;
    }
    if (n % 2 == 0) {
        int c = (bpow(a, n / 2) + mod) % mod;
        return c * c;
    }
    return (bpow(a, n - 1) * a + mod) % mod;
}

void solve() {
    int n, qe;
    cin >> n >> qe;

    vector<int> p(n);
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
    }

    vector<int> orig = p;

    vector<int> b(qe);
    for (int i = 0; i < qe; ++i) {
        cin >> b[i];
    }

    auto rev = [&](int t) {
        vector<int> q(n, 0);
        if (t == 1) {
            for (int i = 0; i < n; ++i) {
                q[n - i - 1] = p[i];
            }
        } else if (t == 2) {
            for (int i = 0; i < n; ++i) {
                q[i] = n - p[i] + 1;
            }
        } else if (t == 3) {
            for (int i = 0; i < n; ++i) {
                q[p[i] - 1] = i + 1;
            }
        }
        return q;
    };

    auto calc = [&]() {
        int c = 0;
        for (int i = 0; i < n; ++i) {
            c = (c + bpow(p[i], i + 1)) % mod;
        }
        return ((c + mod) % mod + mod) % mod;
    };


    set<vector<int> > ss;
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 2; ++j) {
            for (int k = 0; k < 2; ++k) {
                ss.insert(p);
                p = rev(3);
            }
            p = rev(2);
        }
        p = rev(1);
    }
    vector<vector<int> > s;
    sort(all(s));
    for (auto i: ss) {
        s.push_back(i);
    }

    vector<vector<int> > g(s.size());
    vector<int> ans(s.size());

    for (int i = 0; i < s.size(); ++i) {
        p = s[i];
        ans[i] = calc();
        for (int j = 0; j < 3; ++j) {
            p = rev(j + 1);
            g[i].push_back(find(all(s), p) - s.begin());
            p = rev(j + 1);
        }
    }

    int cur = find(all(s), orig) - s.begin();
    for (int i = 0; i < qe; ++i) {
        cur = g[cur][b[i] - 1];
        cout << ans[cur] << " ";
    }
}


signed main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int _t = 1;
    //cin>>_t;
    while (_t--) {
        solve();
    }
    return 0;
}
