#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>
#include <algorithm>
#include <chrono>
#include <random>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define its(a) a.begin(), a.end()
#define scan(a) {for (int i = 0; i < a.size(); ++i) cin >> a[i];}
constexpr ll INFLL = 1e18 + 130;
constexpr int INFINT = 1e9 + 13;
constexpr int MAXN = 1e6;
constexpr int LOGN = 18;
constexpr int M = 998'244'353;
std::mt19937 rnd(std::chrono::high_resolution_clock::now().time_since_epoch().count());

vector <int> get1(vector <int> p) {
	vector <int> q(p.size());
	int n = q.size();
	for (int i = 0; i < n; ++i) {
		q[i] = p[n - i - 1];
	}
	return q;
}

vector <int> get2(vector <int> p) {
	vector <int> q(p.size());
	int n = q.size();
	for (int i = 0; i < n; ++i) {
		q[i] = n - p[i] - 1;
	}
	return q;
}

vector <int> get3(vector <int> p) {
	vector <int> q(p.size());
	int n = q.size();
	for (int i = 0; i < n; ++i) {
		q[p[i]] = i;
	}
	return q;
}

map <vector <int>, int> get_hesh;

void solve() {
	int n; cin >> n;
	vector <int> p(n);
	for (int i = 0; i < n; ++i) p[i] = i;
	for (int i : p) cout << "! " << i;
	cout << endl;
	shuffle(its(p), rnd);
	set <vector <int>> s;
	int quer; cin >> quer;
	vector <set<pair <int, int>>> g(10);
	int cnt = 2;
	get_hesh[p] = 1;
	vector <int> m;
	while (quer--) {
		int v = get_hesh[p];
		int b = uniform_int_distribution<int>(1, 3)(rnd);
		if (b == 1) p = get1(p);
		if (b == 2) p = get2(p);
		if (b == 3) p = get3(p);
		int u = get_hesh[p];
		m.push_back(b);
		if (u == 0) {
			get_hesh[p] = cnt++;
			u = cnt - 1;
		}
		g[v].insert({u, b});
		s.insert(p);
	}
	int all = 0;
	for (int u = 0; u < g.size(); ++u) {
		for (auto t: g[u]) {
			cout << u << " " << t.first << " " << t.second << "\n";
		}
		all += g[u].size();
	}
	cout << s.size() << " " << all << endl;
	for (int i : m) cout << "! " << i;
}

vector <int> ans(10, -1);
vector <vector <pair <int, int>>> g;

int binPow(int a, int n) {
	if (!n) return 1;
	if (n & 1) return 1ll * a * binPow(a, n - 1) % M;
	return binPow(1ll * a * a % M, n / 2);
}

int get(vector <int>& p) {
	int res = 0;
	for (int i = 0; i < p.size(); ++i) {
		res += binPow(p[i] + 1, i + 1);
		if (res >= M) res -= M;
	}
	return res;
}

vector <vector <int>> getP(10);

void sovleVolvo() {
	int n, m; cin >> n >> m;
	vector <int> p(n);
	for (int i = 0; i < n; ++i) {
		cin >> p[i]; p[i]--;
	}
	int cur = 0;
	getP[1] = p;
	ans[1] = get(p);
	for (int v = 1; v < 9; ++v) {
		for (auto u : g[v]) {
			if (ans[u.first] == -1) {
				if (u.second == 1) getP[u.first] = get1(getP[v]);
				if (u.second == 2) getP[u.first] = get2(getP[v]);
				if (u.second == 3) getP[u.first] = get3(getP[v]);
				ans[u.first] = get(getP[u.first]);
			}
		}
	}
	int v = 1;
	while (m--) {
		int b; cin >> b;
		for (auto u : g[v]) {
			if (u.second == b) {
				v = u.first;
				cout << ans[v] << "!\n";
			}
		}
	}
}

void solve2() {
	int n, m; cin >> n >> m;
	vector <int> p(n);
	for (int i = 0; i < n; ++i) {
		cin >> p[i]; p[i]--;
	}
	vector <vector <pair <int, int>>> g(MAXN);
	vector <int> ans(MAXN);
	vector <vector <int>> getP(MAXN);
	ans[0] = get(p);
	getP[0] = p;
	int v = 0;
	int cnt = 1;
	while (m--) {
		int b; cin >> b;
		bool found = false;
		for (auto u : g[v]) {
			if (u.second == b) {
				v = u.first;
				found = true;
				break;
			}
		}
		if (!found) {
			if (b == 1) getP[cnt] = get1(getP[v]);
			if (b == 2) getP[cnt] = get2(getP[v]);
			if (b == 3) getP[cnt] = get3(getP[v]);
			ans[cnt] = get(getP[cnt]);
			v = cnt++;
		}
		cout << ans[v] << " ";
	}
}

int main() {
	cin.tie(nullptr)->sync_with_stdio(false);
	int t_ = 1; //cin >> t_;
	while (t_--) {
		solve2();
	}
}