#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
int mod = 998244353;
#define o_set tree<int, null_type,less<int>, rb_tree_tag, tree_order_statistics_node_update>
using namespace std;
#define int long long
long long bin_pow(int x, int pow) {
    if (!pow)return 1;
    if (pow & 1)return x * bin_pow(x, pow - 1) % mod;
    else {
        return bin_pow(x * x, pow / 2) % mod;
    }
}
signed main() {
    int n, q;
    cin >> n >> q;
    vector<int>a(n);
    for (int i = 0;i < n;i++)cin >> a[i];
    while (q--) {
        int t;
        cin >> t;
        vector<int>b(n);
        if (t == 1) {
            for (int i = 0;i < n;i++) {
                b[i] = a[n - i - 1];
            }
        }else if (t == 2) {

            for (int i = 0;i < n;i++) {
                b[i] = n - a[i] +1 ;
            }
        }else {

            for (int i = 0;i < n;i++) {
                b[a[i] - 1] = i + 1;
            }
        }
        long long ans = 0;
        swap(a, b);
        for (int i = 0;i < n;i++) {
            ans += bin_pow(a[i],  i + 1);
            ans %= mod;
        }
        cout << ans << endl;
    }
}