#include <bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr ll MOD = 998244353;

ll powm(ll a, ll b) {
    if (b == 0) return 1;
    ll v = powm(a, b >> 1);
    if (b % 2 == 0) return (v * v) % MOD;
    return ((v * v) % MOD * a) % MOD;
}

void solve() {
    auto f1 = [&](vector<ll> &a) -> vector<ll> {
        vector<ll> b = a;
        reverse(b.begin(), b.end());
        return b;
    };
    auto f2 = [&](vector<ll> &a) -> vector<ll> {
        vector<ll> b(a.size());
        for (int i = 0; i < a.size(); ++i) {
            b[i] = a.size() - a[i] + 1;
        }
        return b;
    };
    auto f3 = [&](vector<ll> &a) -> vector<ll> {
        vector<ll> b(a.size());
        for (int i = 0; i < a.size(); ++i) {
            b[a[i] - 1] = i + 1;
        }
        return b;
    };
    auto get = [&](vector<ll> &a) -> ll {
        ll ans = 0;
        for (int i = 0; i < a.size(); ++i) ans += powm(a[i], i + 1), ans %= MOD;
        return ans;
    };
    ll n, q;
    cin >> n >> q;
    vector<ll> a(n);
    for (auto &i: a) cin >> i;
    vector<ll> b(q);
    for (auto &i: b) cin >> i;
    ll cnt0 = get(a);
    ll v = b[0];
    auto c = a;
    if (v == 1) c = f1(c);
    else if (v == 2) c = f2(c);
    else c = f3(c);
    ll cnt1 = get(c);
    for (int i = 0; i < q; ++i) {
        if (i % 2 == 0) {
            cout << cnt1 << ' ';
        } else {
            cout << cnt0 << ' ';
        }
    }
    cout << '\n';
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
#ifdef LOCAL
    freopen("input", "r", stdin);
    freopen("output", "w", stdout);
#endif
    solve();
}
