#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>


// order_of_key
// find_by_order
using namespace __gnu_pbds;
using namespace std;
using ll = long long;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;



















const ll mod = 998244353;



ll bin_pow(ll n, ll st) {
	if (st == 0) {
		return 1;
	}

	if (st % 2 == 0) {
		ll rs = bin_pow(n, st / 2);
		return (rs * rs) % mod;
	} else {
		ll rs = bin_pow(n, st / 2);
		return (((rs * rs) % mod) * n) % mod;
	}
}


ll n, q;
	
ll proccessing(ll a, ll b, ll c, vector <ll> p) {
	vector <ll> qp = p;

	for (ll _ = 0; _ < a; _++) {
		for (ll i = 1; i <= n; i++) {
			qp[i] = p[n - i + 1];
		}
	}
	p = qp;
	for (ll _ = 0; _ < b; _++) {
		for (ll i = 1; i <= n; i++) {
			qp[i] = n - p[i] + 1;
		}
	}
	p = qp;
	for (ll _ = 0; _ < c; _++) {
		for (ll i = 1; i <= n; i++) {
			qp[p[i]] = i;
		}
	}
	ll sum = 0;
	p = qp;
	// cout << a << ' ' << b << ' ' << c << endl;
	// for (ll i = 1; i <= n; i++) {
	// 	cout << qp[i] << ' ';
	// }
	// cout << endl;

	for (ll i = 1; i <= n; i++) {
		sum += bin_pow(qp[i], i);
		sum %= mod;
	}
	return sum;
}











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


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



	map <vector <ll>, ll> memo;


	for (ll a = 0; a <= 2; a++) {
		for (ll b = 0; b <= 2; b++) {
			for (ll c = 0; c <= 2; c++) {
				memo[{a, b, c}] = proccessing(a, b, c, p);
			}
		}
	}




	vector <ll> cur = {0, 0, 0, 0};

	while (q--) {
		ll b; cin >> b;

		cur[b]++;
		cur[b] %= 3;

		cout << memo[{cur[1], cur[2], cur[3]}] << ' ';
	}
}



