#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;
using ll = long long;
using ld = long double;
#define endl '\n'
const int mod = 998244353;
int pw(int x, int k) {
	int ans = 1;
	while (k) {
		if (k & 1) ans = (ans * 1ll * x) % mod;
		x = (x * 1ll * x) % mod; k >>= 1;
	}
	return ans;
}
vector<int> first(vector<int> p) {
	reverse(p.begin(), p.end());
	return p;
}
vector<int> second(vector<int> p) {
	int n = p.size();
	vector<int> q(n);
	for (int i = 0; i < n; i++) q[i] = n + 1 - p[i];
	return q;
}
vector<int> third(vector<int> p) {
	int n = p.size();
	vector<int> q(n);
	for (int i = 0; i < n; i++) q[p[i] - 1] = i + 1;
	return q;
}
int get(vector<int> v) {
	int res = 0;
	for (int i = 0; i < v.size(); i++) res = (res + pw(v[i], i + 1)) % mod;
	return res;
}
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n, q; cin >> n >> q;
	vector<int> p(n);
	for (auto& i : p) cin >> i;
	vector<int> dp(64);
	dp[0] = get(p);
	for (int mask = 1; mask < 64; mask++) {
		vector<int> q = p;
		vector<int> op; int x = mask;
		for (int i = 0; i < 3; i++) {
			op.push_back(x % 4); x /= 4;
		}
		reverse(op.begin(), op.end()); bool fl = 0;
		for (int i = 0; i < 3; i++) {
			if (op[i] == 0) {
				for (int j = i + 1; j < 3; j++) {
					if (op[j] != 0) {
						fl = 1; break;
					}
				}
				if (fl) break;
			}
		}
		if (fl) continue;
		for (int i = 0; i < 3; i++) {
			if (op[i] == 0) break;
			if (op[i] == 1) {
				q = first(q);
			} else if (op[i] == 2) {
				q = second(q);
			} else {
				q = third(q);
			}
		}
		dp[mask] = get(q);
	}
	vector<int> op;
	vector<int> ans;
	while (q--) {
		int tp; cin >> tp;
		if (tp == 1) {
			p = first(p);
		}
		else if (tp == 2) {
			p = second(p);
		}
		else {
			p = third(p);
		}
		//for (int i = 0; i < n; i++) cout << p[i] << " ";
		//cout << endl;
		ans.push_back(get(p));
	}
	for (auto el : ans) cout << el << " ";
	return 0;
}
