/*
#include <bits/stdc++.h>

using namespace std;

constexpr long long mod = 998244353;

long long bin_pow(long long a, long long b) {
    if (b == 0) return 1;
    if (b % 2 == 0) return bin_pow((a * a) % mod, b / 2);
    else return (bin_pow(a, b - 1) * a) % mod;
}

long long count_ans(vector<long long>& a) {
    long long n = a.size();
    long long sum = 0;
    for (long long i = 0; i < n; ++i) {
        sum += bin_pow(a[i], i + 1);
        if (sum >= mod) sum -= mod;
    }
    return sum;
}

vector<long long> solve1(vector<long long> a) {
    long long n = a.size();
    vector<long long> b = a;
    reverse(b.begin(), b.end());
    return b;
}

vector<long long> solve2(vector<long long> a) {
    long long n = a.size();
    vector<long long> b(n);
    for (long long i = 0; i < n; ++i) b[i] = n + 1 - a[i];
    return b;
}

vector<long long> solve3(vector<long long> a) {
    long long n = a.size();
    vector<long long> b(n);
    for (long long i = 0; i < n; ++i) b[a[i] - 1] = i + 1;
    return b;
}

void delet(vector<long long>& a) {
	long long n = a.size();
	if (n >= 2) {
		if (a[n - 1] == a[n - 2]) {
			a.pop_back();
			a.pop_back();
			return;
		}
	}
	if (n >= 3) {
		if (a[n - 3] == 3 && a[n - 1] == 3) {
			long long num = a[n - 2];
			a.pop_back();
			a.pop_back();
			a.pop_back();
			a.push_back(num);
			delet(a);
		}
		if (a[n - 3] == 1 && a[n - 2] == 2 && a[n - 1] == 1) {
			a.pop_back();
			a.pop_back();
			a.pop_back();
			a.push_back(2);
			delet(a);
		}
		if (a[n - 3] == 2 && a[n - 2] == 1 && a[n - 1] == 2) {
			a.pop_back();
			a.pop_back();
			a.pop_back();
			a.push_back(2);
			delet(a);
		}
	}
	if (n >= 4) {
        if (a[n - 4] == 3 && a[n - 1] == 3) {
            a.pop_back();
            a.pop_back();
            a.pop_back();
            a.pop_back();
            a.push_back(1);
            a.push_back(2);
            delet(a);
        }
	}
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    map<vector<long long>, long long> solved;
    long long n, q;
    cin >> n >> q;
    vector<long long> a(n);
    for (long long i = 0; i < n; ++i) a[i] = i + 1;
    random_shuffle(a.begin(), a.end());
    vector<long long> st1, st2;
    while (q--) {
        x = rnd() % 3 + 1;
        st1.push_back(x);
        st2.push_back(x);
        delet(st1);
        if (true) {
            vector<long long> a1 = a, a2 = a;
            for (auto x : st1) {
                if (x == 1) a1 = solve1(a1);
                if (x == 2) a1 = solve2(a1);
                if (x == 3) a1 = solve3(a1);
            }
            for (auto x : st2) {
                if (x == 1) a2 = solve1(a2);
                if (x == 2) a2 = solve2(a2);
                if (x == 3) a2 = solve3(a2);
            }
            long long num1 = count_ans(a1);
            long long num2 = count_ans(a2);
            if (num1 != num2) {
                
            }
            if (num == 0) num += mod;
            solved[st1] = num;
        }
        cout << (solved[st1] % mod) << ' ';
    }
}
*/

#include <bits/stdc++.h>

using namespace std;

constexpr long long mod = 998244353;

long long bin_pow(long long a, long long b) {
	if (b == 0) return 1;
	if (b % 2 == 0) return bin_pow((a * a) % mod, b / 2);
	else return (bin_pow(a, b - 1) * a) % mod;
}

long long count_ans(vector<long long>& a) {
	long long n = a.size();
	long long sum = 0;
	for (long long i = 0; i < n; ++i) {
		sum += bin_pow(a[i], i + 1);
		if (sum >= mod) sum -= mod;
	}
	return sum;
}

vector<long long> solve1(vector<long long> a) {
	long long n = a.size();
	vector<long long> b = a;
	reverse(b.begin(), b.end());
	return b;
}

vector<long long> solve2(vector<long long> a) {
	long long n = a.size();
	vector<long long> b(n);
	for (long long i = 0; i < n; ++i) b[i] = n + 1 - a[i];
	return b;
}

vector<long long> solve3(vector<long long> a) {
	long long n = a.size();
	vector<long long> b(n);
	for (long long i = 0; i < n; ++i) b[a[i] - 1] = i + 1;
	return b;
}

void delet(vector<long long>& a) {
	long long n = a.size();
	if (n >= 2) {
		if (a[n - 1] == a[n - 2]) {
			a.pop_back();
			a.pop_back();
			return;
		}
	}
	if (n >= 3) {
		if (a[n - 3] == 3 && a[n - 1] == 3) {
			long long num = a[n - 2];
			a.pop_back();
			a.pop_back();
			a.pop_back();
			a.push_back(num);
			delet(a);
		}
		if (a[n - 3] == 1 && a[n - 2] == 2 && a[n - 1] == 1) {
			a.pop_back();
			a.pop_back();
			a.pop_back();
			a.push_back(2);
			delet(a);
		}
		if (a[n - 3] == 2 && a[n - 2] == 1 && a[n - 1] == 2) {
			a.pop_back();
			a.pop_back();
			a.pop_back();
			a.push_back(1);
			delet(a);
		}
	}
	if (n >= 4) {
		if (a[n - 4] == 3 && a[n - 1] == 3) {
			a.pop_back();
			a.pop_back();
			a.pop_back();
			a.pop_back();
			a.push_back(1);
			a.push_back(2);
			delet(a);
		}
	}
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	map<vector<long long>, long long> solved;
	long long n, q;
	cin >> n >> q;
	vector<long long> a(n);
	for (long long i = 0; i < n; ++i) cin >> a[i];
	vector<long long> st, st1;
	while (q--) {
		long long x; cin >> x;
		st.push_back(x);
		st1.push_back(x);
		delet(st);
		if (solved[st] == 0) {
			vector<long long> a1 = a;
			for (auto x : st) {
				if (x == 1) a1 = solve1(a1);
				if (x == 2) a1 = solve2(a1);
				if (x == 3) a1 = solve3(a1);
			}
			long long num = count_ans(a1);
			if (num == 0) num += mod;
			solved[st] = num;
		}
		cout << (solved[st] % mod) << ' ';
	}
}
