#pragma optimize("O3")
#pragma target("avx2, popcnt")
#include <bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;

#define int long long

const int mod = 998'244'353;
int t[2][2][2][2][2];

int binpow (int x, int n) {
    int res = 1;
    while (n) {
        if (n & 1) res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}

void solve()  {
    int n, m;
    cin >> n >> m;
    vector<int> a(n), b(m);
    for (int& i : a) cin >> i;
    for (int& i : b) cin >> i;
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 2; ++j) {
            for (int k = 0; k < 2; ++k) {
                for (int d = 0; d < 2; ++d) {
                    vector<int> a2 = a;
                    if (i) reverse(a2.begin(), a2.end());
                    if (j) for (int o = 0; o < n; ++o) a2[o] = n - a2[o] + 1;
                    int sum = 0;
                    for (int o = 0; o < n; ++o) sum = (sum + binpow(a2[o], o + 1)) % mod;
                    t[i][j][0][k][d] = sum; sum = 0;
                    auto a3 = a2;
                    for (int o = 0; o < n; ++o) a2[a3[o] - 1] = o + 1;
                    if (k) reverse(a2.begin(), a2.end());
                    if (d) for (int o = 0; o < n; ++o) a2[o] = n - a2[o] + 1;
                    for (int o = 0; o < n; ++o) sum = (sum + binpow(a2[o], o + 1)) % mod;
                    t[i][j][1][k][d] = sum;
                }
            }
        }
    } // 3 ? t[i][j][k][d] : t[i][j]
    int flag = 0, l1 = 0, l2 = 0, r1 = 0, r2 = 0;
    for (int i : b) {
        if (flag && i != 3)
            r1 = (r1 + (i == 1)) % 2, r2 = (r2 + (i == 2)) % 2;
        else if (flag && i == 3) {
            l1 = (l1 + r2) % 2, l2 = (l2 + r1) % 2;
            r1 = 0, r2 = 0;
            flag = 0;
        } else if (!flag && i == 3) {
            flag = 1;
        } else l1 = (l1 + (i == 1)) % 2, l2 = (l2 + (i == 2)) % 2;
        cerr << l1 << ' ' << l2 << ' ' << flag << ' ' << r1 << ' ' << r2 << endl;
        cout << t[l1][l2][flag][r1][r2] << ' ';
    }
}

int32_t main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    cin.tie(nullptr);
    cout.tie(nullptr);
    ios::sync_with_stdio(false);
    int test = 1; /*cin >> test*/
    while (test--)
        solve();
}