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

#ifndef LOCAL
#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2")
#endif
const int mod = 998244353;
int binpow(int n, int p) {
    if (p == 0)
        return 1;
    if (p % 2 == 0) {
        int tmp = binpow(n, p / 2);
        return tmp * tmp % mod;
    }
    return n * binpow(n, p - 1) % mod;
}
signed main() {
    ios_base::sync_with_stdio(NULL);
    cin.tie(NULL);
#ifdef LOCAL
    freopen("./../input.txt", "r", stdin);
#endif
    int n, q;
    cin >> n >> q;
    vector<int> a(n);
    vector<int> b(q);
    for (auto& x : a) cin >> x;
    for (auto& x : b) cin >> x;
    auto calc = [&]() -> int {
        int res = 0;
        for (int i = 0; i < n; i++) {
            res += binpow(a[i], i + 1);
        }
        res %= mod;
        return res;
    };
    auto rev = [&]() -> void {
        reverse(a.begin(), a.end());
    };
    auto pev = [&]() -> void {
        for (int i = 0; i < n; i++) {
            a[i] = n - a[i] + 1;
        }
    };
    auto inv = [&]() -> void {
        vector<int> tmp(n);
        for (int i = 0; i < n; i++) {
            tmp[a[i] - 1] = i + 1;
        }
        swap(tmp, a);
    };

    vector<int> A(n);
    copy(a.begin(), a.end(), A.begin());

    vector<vector<vector<int>>> dp(2, vector<vector<int>>(2, vector<int>(2)));
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            for (int k = 0; k < 2; k++) {
                copy(A.begin(), A.end(), a.begin());
                for (int t = 0; t < i; t++) {
                    rev();
                }
                for (int t = 0; t < j; t++) {
                    pev();
                }
                for (int t = 0; t < k; t++) {
                    inv();
                }
                dp[i][j][k] = calc();
            }
        }
    }
    vector<int> cnt(n);
    int cnt1 = 0, cnt2 = 0, cnt3 = 0;
    for (int i = 0; i < q; i++) {
        if (b[i] == 1 && cnt3 == 0 || b[i] == 2 && cnt3 == 1) {
            cnt1++;
            cnt1 %= 2;
        } else if (b[i] != 3) {
            cnt2++;
            cnt2 %= 2;
        } else {
            cnt3++;
            cnt3 %= 2;
        }
        cout << dp[cnt1][cnt2][cnt3] << " ";
    }
    cout << endl;
    return 0;
}