#pragma GCC optimize("O3")
#include <vector>

#pragma GCC target("avx2")
#include <algorithm>
#include <random>
#include <numeric>
#include <iostream>
#include <stack>
#include <queue>
#include <deque>
#include <string>
#include <set>
#include <map>

using namespace std;

#define ll long long
#define ld long double
#define pll pair<ll, ll>
#define pld pair<ld, ld>

#define vll vector<ll>
#define vvll vector<vll>
#define vvvll vector<vvll>
#define vvvvll vector<vvvll>

#define vld vector<ld>
#define vvld vector<vld>
#define vvvld vector<vvld>
#define vvvvld vector<vvvld>

#define vpll vector<pll>
#define vvpll vector<vpll>
#define vvvpll vector<vvpll>
#define vvvvpll vector<vvvpll>

#define vpld vector<pld>
#define vvpld vector<vpld>
#define vvvpld vector<vvpld>
#define vvvvpld vector<vvvpld>

#define forlr(i, l, r) for(ll i = (l); i < (r); ++i)
#define forn(i, n) forlr(i, 0, n)
#define forin(x, v) for(auto& x : v)

#define all(v) v.begin(), v.end()
#define pb emplace_back

template <class T>
istream& operator>>(istream& in, vector<T>& v) {
	forin(x, v)
		in >> x;
	return in;
}

template <class T>
ostream& operator<<(ostream& out, vector<T>& v) {
	forin(x, v)
		out << x << ' ';
	return out;
}

const ll MOD = 998244353;

ll pow(ll a, ll n) {
	if (!n)
		return 1;
	if (n % 2)
		return a * pow(a, n - 1) % MOD;
	return pow(a * a % MOD, n / 2);
}

ll ask(vll a) {
	ll n = a.size();
	ll ans = 0;
	forn(i, n) {
		ans = (ans + pow(a[i], i + 1)) % MOD;
	}
	return ans;
}

vll op1(vll a) {
	reverse(all(a));
	return a;
}

vll op2(vll a) {
	ll n = a.size();
	forn(i, n)
		a[i] = n - a[i] + 1;
	return a;
}

vll op3(vll a) {
	ll n = a.size();
	vll ans(n);
	forn(i, n)
		ans[a[i] - 1] = i + 1;
	return ans;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

#ifdef _LOCAL
	while (1)
#endif // _LOCAL
	{
		ll t = 1;
		//cin >> t;
		while (t--) {
			ll n, m;
			cin >> n >> m;
			vll a(n), b(m);
			cin >> a >> b;

			
			while (0) {
				ll x;
				cin >> x;
				if (x == 1)
					a = op1(a);
				if (x == 2)
					a = op2(a);
				if (x == 3)
					a = op3(a);
				cout << a << '\n';
			}

			/*mt19937 mt;

			iota(all(a), 1);

			shuffle(all(a), mt);

			set<vll> s;
			s.insert(a);

			forn(i, 1000) {
				ll x = mt() % 3 + 1;
				if (x == 1)
					a = op1(a);
				if (x == 2)
					a = op2(a);
				if (x == 3)
					a = op3(a);
				s.insert(a);
			}
			cout << s.size();*/

			ll is1 = 1;

			vll ans(8);
			ans[0] = ask(a);
			ans[1] = ask(op1(a));
			ans[2] = ask(op2(a));
			ans[3] = ask(op1(op2(a)));
			ans[4] = ask(op3(a));

			if (!is1) {
				ans[5] = ask(op2(op3(a)));
				ans[6] = ask(op1(op3(a)));
			}
			else {
				ans[5] = ask(op1(op3(a)));
				ans[6] = ask(op2(op3(a)));
			}


			ans[7] = ask(op1(op2(op3(a))));

			ll f = 0;
			forin(x, b) {
				f ^= (1ll << x - 1);
				cout << ans[f] << ' ';
			}

			cout << endl;
		}
	};
}
