#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <numeric>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <bitset>
#include <random>
#include <queue>
using namespace std;
#pragma GCC target("avx,avx2,tune=native,sse,sse2")
#define int long long

const int mod = 998244353;

inline bool vsuprun() {
	static int it = 0;
	static bool done = false;
	if (it++ == 100) {
		it = 0;
		if (1.0 * clock() / CLOCKS_PER_SEC > 1.0) {
			done = true;
		}
	}
	return done;
}

int binpow(int a, int b) {
	if (b == 0) {
		return 1;
	}
	int t = binpow(a, b / 2);
	if (b % 2 == 0) {
		return (t * t) % mod;
	}
	return (((t * t) % mod) * a) % mod;
}

signed main() {
	map<vector<int>, int> mp;
	int n, q;
	cin >> n >> q;
	vector<int> p(n);
	for (int i = 0; i < n; ++i) {
		cin >> p[i];
	}
	bool fl = 1;
	vector<pair<vector<int>, vector<int>>> per;
	vector<pair<vector<int>, vector<int>>> new_per;
	per.push_back({ {}, p });
	mp[p] = 1;
	int prev = 1;
	while (fl == 1) {
		fl = 0;
		for (auto pp : per) {
			/*for (auto i : pp) {
				cout << i << ' ';
			}
			cout << endl;*/
			vector<int> b1(n);
			vector<int> b2(n);
			vector<int> b3(n);
			for (int i = 0; i < n; ++i) {
				b1[i] = pp.second[n - i - 1];
			}
			for (int i = 0; i < n; ++i) {
				b2[i] = n - pp.second[i] + 1;
			}
			for (int i = 0; i < n; ++i) {
				b3[pp.second[i] - 1] = i + 1;
			}
			/*cout << "b1" << endl;
			for (auto i : b1) {
				cout << i << ' ';
			}
			cout << endl;
			cout << "b2" << endl;
			for (auto i : b2) {
				cout << i << ' ';
			}
			cout << endl;
			cout << "b3" << endl;
			for (auto i : b3) {
				cout << i << ' ';
			}
			cout << endl;*/
			if (mp[b1] == 0) {
				mp[b1] = prev + 1;
				prev++;
				fl = 1;
				vector<int> h = pp.first;
				h.push_back(1);
				new_per.push_back({ h, b1 });
			}
			if (mp[b2] == 0) {
				mp[b2] = prev + 1;
				prev++;
				fl = 1;
				vector<int> h = pp.first;
				h.push_back(2);
				new_per.push_back({ h, b2 });
			}
			if (mp[b3] == 0) {
				mp[b3] = prev + 1;
				prev++;
				fl = 1;
				vector<int> h = pp.first;
				h.push_back(3);
				new_per.push_back({ h, b3 });
			}
		}
		for (auto i : new_per) {
			per.push_back(i);
		}
		new_per.clear();
	}
	/*cout << per.size() << endl;
	for (auto i : per) {
		for (auto j : i.second) {
			cout << j << ' ';
		}
		cout << endl;
		cout << "path" << endl;
		for (auto j : i.first) {
			cout << j << ' ';
		}
		cout << endl;
	}*/
	vector<vector<int>> g(per.size(), vector<int>(3));
	for (int j = 0; j < per.size(); ++j) {
		vector<int> b1(n);
		vector<int> b2(n);
		vector<int> b3(n);
		for (int i = 0; i < n; ++i) {
			b1[i] = per[j].second[n - i - 1];
		}
		for (int i = 0; i < n; ++i) {
			b2[i] = n - per[j].second[i] + 1;
		}
		for (int i = 0; i < n; ++i) {
			b3[per[j].second[i] - 1] = i + 1;
		}
		g[j][0] = mp[b1] - 1;
		g[j][1] = mp[b2] - 1;
		g[j][2] = mp[b3] - 1;
	}
	/*for (auto i : g) {
		cout << i[0] << ' ' << i[1] << ' ' << i[2] << endl;
	}*/
	vector<int> ans(per.size());
	for (int j = 0; j < per.size(); ++j) {
		int res = 0;
		for (int i = 0; i < n; ++i) {
			res += binpow(per[j].second[i], i + 1);
			res %= mod;
		}
		ans[j] = res;
	}
	int s = 0;
	vector<int> b(q);
	for (int i = 0; i < q; ++i) {
		cin >> b[i];
		b[i]--;
	}
	for (auto i : b) {
		s = g[s][i];
		cout << ans[s] << ' ';
	}
	cout << endl;
	return 0;
}