#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#define all(x) x.begin(), x.end()

const ll MOD = 998244353;


struct mint {
    ll x;

    mint(ll s = 0) {
        x = s;
    }

    mint operator+(mint other) {
        mint res;
        res.x = x + other.x;
        if (res.x > MOD) {
            res.x -= MOD;
        }
        return res;
    }

    mint operator-(mint other) {
        mint res;
        res.x = x - other.x;
        if (res.x < 0) {
            res.x += MOD;
        }
        return res;
    }

    mint operator*(mint other) {
        mint res;
        res.x = x * other.x;
        res.x %= MOD;
        return res;
    }

    mint binpow(ll n) {
        if (n == 0) return 1;
        if (n == 1) {
            return (*this);
        }
        if (n % 2 == 0) {
            mint lr = binpow(n / 2);
            return lr * lr;
        }
        return (*this) * binpow(n - 1);
    }
};


ll comply(vector<ll> & p) {
    mint ans = 0;
    for (int i = 1; i < p.size(); ++i) {
        ans = ans + mint(p[i]).binpow(i);
    }
    return ans.x;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

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

    bool is_rev = 0;
    ll b;
    for (int op = 0; op < q; ++op) {
        cin >> b;
    }

    ll res = comply(p);
    if (b == 1) {
        reverse(all(p));
        vector<ll> rcs(n + 1);
        for (int i = 0; i < n; ++i) {
            rcs[i + 1] = n - i;
        }
        ll r2 = comply(rcs);
        for (int op = 0; op < q; ++op) {
            if (op % 2 == 0) {
                cout << r2 << ' ';
            } else {
                cout << res << ' ';
            }
        }
    } else if (b == 2) {
        vector<ll> nq(n + 1);
        for (int i = 1; i <= n; ++i) {
            nq[i] = n - p[i] + 1;
        }
        ll r2 = comply(nq);
        for (int op = 0; op < q; ++op) {
            if (op % 2 == 0) {
                cout << r2 << ' ';
            } else {
                cout << res << ' ';
            }
        }
    } else {
        vector<ll> nq(n + 1);
        for (int i = 1; i <= n; ++i) {
            nq[p[i]] = i;
        }
        ll r2 = comply(nq);
        for (int op = 0; op < q; ++op) {
            if (op % 2 == 0) {
                cout << r2 << ' ';
            } else {
                cout << res << ' ';
            }
        }
    }
}