#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <climits>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <vector>
#pragma GCC optimize("Ofast")
using namespace std;
using ll = long long;
using ld = long double;
const int maxn = 5'000, mod = 998'244'353;
int pv[maxn][maxn];
ll pp(ll val, ll pp1) {
	ll ans = 1;
	ll start = val;
	while (pp1 != 1) {
		if (pp1 % 2 == 1) {
			ans = (ans * start) % mod;
			pp1--;
		}
		else {
			ans = (ans * ((start * start) % mod)) % mod;
 			pp1 /= 2;
			start = ans;
		}
	}
	return ans;
}
ll calc(vector<int> &a) {
	ll ans = 0;
	for (int i = 0; i < a.size(); ++i) {
		ll cur = pv[a[i]][i+1];
		ans += cur;
	}
	return ans;
}
int main() {
	cin.tie(0)->sync_with_stdio(false);
	int n, q; cin >> n >> q;
	vector<int> a(n);
	for (int i = 0; i < n; ++i) cin >> a[i];
	for (int i = 0; i < 2000; ++i) {
		pv[i][0] = 1;
		for (int j = 1; j < 2000; ++j) {
			pv[i][j] = (pv[i][j - 1] * i) % mod;
		}
	}
	for (int i = 0; i < q; ++i) {

		int cmd; cin >> cmd;
		if (cmd == 1) {
			reverse(a.begin(), a.end());
		}
		else if (cmd == 2) {
			vector<int> b(n);
			for (int i = 0; i < n; ++i) {
				b[i] = n - a[i] + 1;
			}
			swap(a, b);
		}
		else {
			vector<int> b(n);
			for (int i = 0; i < n; ++i) {
				b[a[i] - 1] = i + 1;
			}
			swap(a, b);
		}
		cout << calc(a) << ' ';
	}
}