#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define int int64_t
#define vi vector<int>
#define vvi vector<vi>
#define vvvi vector<vvi>
#define pii pair<int, int>
#define vpi vector<pii>
#define vb vector<bool>
#define umap unordered_map
using namespace std;
//using namespace __gnu_pbds;
mt19937 rng(333);
const int mod = 998244353;
//template<class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//template<class T> using omset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
int powmod(int b, int p) {
	int s=1;
	while (p) {
		if (p&1) s = (s*b)%mod;
		b = (b*b)%mod;
		p >>= 1;
	}
	return s;
}
void make(vi& p, vvi& G, vi& res, int cur) {
	int n = p.size();
	res[cur] = 0;
	for (int i=0; i<n; ++i) {
		res[cur] = (res[cur]+powmod(p[i], i+1))%mod;
	}
	if (res[G[cur][0]] == -1) {
		reverse(p.begin(), p.end());
		make(p, G, res, G[cur][0]);
		reverse(p.begin(), p.end());
	}
	if (res[G[cur][1]] == -1) {
		for (int i=0; i<n; ++i) p[i] = n-p[i]+1;
		make(p, G, res, G[cur][1]);
		for (int i=0; i<n; ++i) p[i] = n-p[i]+1;
	}
	if (res[G[cur][2]] == -1) {
		vi temp(n, 0);
		for (int i=0; i<n; ++i) temp[i] = p[i];
		for (int i=0; i<n; ++i) p[temp[i]-1] = i+1;
		make(p, G, res, G[cur][2]);
		for (int i=0; i<n; ++i) p[i] = temp[i];
	}
}
void solve() {
	int n, q; cin >> n >> q;
	vi p(n, 0); for (int i=0; i<n; ++i) cin >> p[i];
	vi res(8, -1);
	vvi G = {{1, 3, 5}, {0, 2, 6}, {3, 1, 7}, {2, 0, 4}, {5, 7, 3}, {4, 6, 0}, {7, 5, 1}, {6, 4, 2}};
	make(p, G, res, 0);
	int x;
	int cur=0;
	for (int i=0; i<q; ++i) {
		cin >> x; x--;
		cur = G[cur][x];
		cout << res[cur] << ' ';
	}
	cout << '\n';
}
signed main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	int t=1; //cin >> t;
	while (t--) solve();
	return 0;
}
