#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

template<class T>
ostream& operator<<(ostream& out, vector<T> v) {
	for (T it : v) {
		out << it << ' ';
	}
	out << '\n';
	return out;
}

#define ll long long

const int MAX = 100'005;
const int MOD = 998'244'353;

ll mpow(ll a, ll x) {
	if (x == 0) {
		return 1;
	}
	if (x & 1) {
		return (a * mpow(a * a % MOD, x / 2)) % MOD;
	}
	return mpow(a * a % MOD, x / 2);
}
int a[MAX];
int b[MAX];
int q[MAX];
int n, m;

void f1() {
	reverse(a, a + n);
}
void f2() {
	for (int i = 0; i < n; i++) {
		a[i] = n - a[i] + 1;
	}
}
void f3() {
	for (int i = 0; i < n; i++) {
		q[a[i] - 1] = i + 1;
	}
	for (int i = 0; i < n; i++) {
		a[i] = q[i];
	}
}
ll get() {
	ll res = 0;
	for (int i = 0; i < n; i++) {
		res += mpow(a[i], i + 1);
		res %= MOD;
	}
	return res;
}
int main ()
{
	cin >> n >> m;

	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int i = 0; i < m; i++) {
		cin >> b[i];
	}
	ll r[2][2];
	r[0][0] = get();
	f1();
	r[1][0] = get();
	f2();
	r[1][1] = get();
	f1();
	r[0][1] = get();
	int i = 0, j = 0;
	for (int it = 0; it < m; it++) {
		if (b[i] == 1) {
			i = 1 - i;
		}
		else {
			j = 1 - j;
		}
		cout << r[i][j] << ' ';
	}
}


