// consoleapp1.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#pragma GCC target ("avx2", "bmi", "popcnt", "lzcnt")
#pragma optimize (Ofast)
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using ll = long long;
using namespace std;
ll MOD = 998244353;
ll binpow(int n, int k) {
	ll res = 1, a = n;
	while (k) {
		if (k & 1) {
			res *= a;
			res %= MOD;
		}
		a *= a;
		a %= MOD;
		k >>= 1;
	}
	return res;
}
int main() {
	ll n, m;
	cin >> n >> m;
	vector<long long> a(n);
	vector<ll> q(m);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	ll ans1 = 0, ans2 = 0;
	for (int i = 1; i <= n; i++) {
		ans1 += binpow(i, i) % MOD;
	}
	for (int i = n; i > 0; i--) {
		ans2 += binpow(i, n - i) % MOD;
	}
	bool Last = 0;
	for (int i = 0; i < n; i++) {
		cin >> q[i];
		////vector<ll> nw(n);
		if ((q[i] == 1 or q[i] == 2) && !Last) {
			/*for (int j = 0; j < n; j++) {*/
				/*nw[j] = a[n - j-1];*/
			cout << ans2 % MOD << " ";
			Last = 1;
		}
		else if (q[i] == 1 or q[i] == 2) {
			cout << ans1 % MOD << " ";
			Last = 0;
		}
		else if (q[i] == 3 && Last == 0) {
			cout << ans1 % MOD << " ";
		}
		else {
			cout << ans2 % MOD << " ";
		}

		/*if (q[i] == 2) {
			for (int j = 0; j < n; j++) {
				nw[j] = n+1-a[j];
			}
		}
		if (q[i] == 3) {
			for (int j = 0; j < n; j++) {
				nw[a[j]-1] =j+1;
			}
		}
		ll ans = 0;
		for (int j = 0; j < n; j++) {
			ans += binpow(nw[j], j + 1) % MOD;
		}
		cout << ans % MOD<<" ";
		if (i == 0) {
			ans1 = ans;
		}
		else {
			ans2 = ans;
		}
		a = nw;
	}
	for (int i = 2; i < m; i++) {
		if (i % 2) {
			cout << ans2%MOD << " ";
		}
		else {
			cout << ans1%MOD << " ";
		}
	}*/

	}
}
