#include <iostream>
#include<vector>
#include<set>
#include<map>
#include <cmath>
#include <algorithm>

using namespace std;
long long typedef ll;
const ll MOD = 998243353;

void operation3(vector<ll>& data, ll n) {
	vector<ll> newdata(n);
	for (ll i = 0; i < n; ++i) {
		newdata[data[i] - 1] = i + 1;
	}
	data = newdata;
}

void operation2(vector<ll>& data, ll n) {
	for (ll i = 0; i < n; ++i) {
		data[i] = n - data[i] + 1;
	}
}

ll fastpow(ll n, ll power) {
	vector<ll> order;
	while (power > 1) {
		if (power % 2 == 0) {
			order.push_back(2);
			power /= 2;
		}
		else {
			order.push_back(1);
			power -= 1;
		}
	}
	ll s = n;
	reverse(order.begin(), order.end());
	for (ll i : order) {
		if (i == 1) {
			s *= n;
			s %= MOD;
		}
		else {
			s = s * s;
			s %= MOD;
		}
	}
	return s;
}

ll countcost(vector<ll>& data, ll n) {
	ll s = 0;
	for (ll i = 0; i < n; ++i) {
		s += fastpow(data[i], i + 1);
		s %= MOD;
	}
	return s;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	ll n, q, ent;
	cin >> n >> q;
	vector<ll> data;
	for (ll i = 0; i < n; ++i) {
		cin >> ent;
		data.push_back(ent);
	}
	ll cnt1=0, cnt2=0, cnt3=0;
	bool test1 = (n <= 1000 && q <= 1000), test2 = true, test3 = true, test4 = true;
	vector<ll> tasks;
	for (ll i = 0; i < q; ++i) {
		cin >> ent;
		if (ent == 1) {
			cnt1 += 1;
		}
		else if (ent == 2) {
			cnt2 += 1;
		}
		else {
			cnt3 += 1;
		}
		if (i > 0 && ent != tasks.back()) {
			test2 = false;
		}
		if (ent == 3) {
			test3 = false;
		}
		if (ent != i + 1) {
			test4 = false;
		}
		tasks.push_back(ent);
	}
	vector<ll> ans;
	for (ll i = 0; i < q; ++i) {
		if (tasks[i] == 1) {
			reverse(data.begin(), data.end());
		}
		else if (tasks[i] == 2) {
			operation2(data, n);
		}
		else {
			operation3(data, n);
		}
		cout << countcost(data, n) << " ";
	}
}
