#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const ll MOD = 998244353;
const int MAXN = 1e5;

ll binpow(ll n, int k) {
    ll res = 1LL;
    while (k) {
        if (k & 1)
            res = (res * n) % MOD;
        n = (n * n) % MOD;
        k >>= 1;
    }
    return res;
}

pair<ll, ll> simple_rev(vector<int> &a, int n) {
    ll s = 0LL, rev = 0LL;
    for (int k = 1; k <= n; ++k) {
        s = (s + binpow(a[k], k)) % MOD;
        rev = (rev + binpow(a[n - k + 1], k)) % MOD;
    }
    return {s, rev};
}

pair<ll, ll> rev2(vector<int> &a, int n) {
    ll s = 0LL, rev = 0LL;
    for (int k = 1; k <= n; ++k)
        s = (s + binpow(a[k], k)) % MOD;

    vector<int> r(n + 1);
    for (int k = 1; k <= n; ++k) {
        r[k] = n - a[k] + 1;
        rev = (rev + binpow(r[k], k)) % MOD;
    }
    return {s, rev};
}

pair<ll, ll> rev3(vector<int> &a, int n) {
    ll s = 0LL, rev = 0LL;
    for (int k = 1; k <= n; ++k)
        s = (s + binpow(a[k], k)) % MOD;

    vector<int> r(n + 1);
    for (int k = 1; k <= n; ++k) {
        r[a[k]] = k;
    }
    for (int k = 1; k <= n; ++k) {
        rev = (rev + binpow(r[k], k)) % MOD;
    }
    return {s, rev};
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int p, q;
    cin >> p >> q;
    vector<int> a(p + 1);
    for (int i = 1; i <= p; ++i) {
        cin >> a[i];
    }

    bool rev = false;

    ll s = 0LL, revs = 0LL;
    for (int k = 1; k <= p; ++k)
        s = (s + binpow(k, k)) % MOD;
    for (int k = 1; k <= p; ++k)
        revs = (revs + binpow(p - k + 1, k)) % MOD;
    int b;
    cin >> b;
    pair<ll, ll> t;
    if (b == 1)
        t = simple_rev(a, p);
    else if (b == 2)
        t = rev2(a, p);
    else
        t = rev3(a, p);
    for (int i = 1; i < q; ++i) cin >> b;
    while (q--) {
        if (rev) cout << t.first;
        else cout << t.second;
        cout << ' ';
        rev = !rev;
    }
}