#include "bits/extc++.h"
//#pragma optimize("O3")
//#pragma target("avx2,popcnt")
//#pragma target("unroll-loops")
using namespace std;
using namespace __gnu_pbds;
using ll = long long;

using ordered_set = tree<int, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update>;

template<typename T>
istream &operator>>(istream &is, vector<T> &a) {
    for (auto &v: a) is >> v;
    return is;
}

template<typename T>
ostream &operator<<(ostream &os, vector<T> &a) {
    for (auto &v: a) os << v << " ";
    return (os << "\n");
}

const int mod = 998244353;

ll binpow(ll a, ll n) {
    ll res = 1;
    while (n) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("ans.txt", "w", stdout);
#else
    ios_base::sync_with_stdio(false);
    cin.tie(0);
#endif

    ll n, q;
    cin >> n >> q;

    vector<ll> a(n);
    cin >> a;

    map<ll, vector<ll>> va;
    map<ll, vector<ll>> move;

    ll cost = 0;
    for (int j = 0; j < n; ++j) {
        cost = (cost + binpow(a[j], j + 1)) % mod;
    }
    
    vector<int> ques(q);
    cin >> ques;
    
    if (std::count(ques.begin(), ques.end(),3) == 0){

        vector<vector<ll>> cost(2,vector<ll>(2));
        for (int f1 = 0; f1 < 2; ++f1) {
            for (int f2 = 0; f2 < 2; ++f2) {
                for (int i = 0; i < n; ++i) {
                    int I = (f1 ? i : n-i-1);
                    int V = (f2 ? a[I] : n-a[I]+1);
                    cost[f1][f2] = (cost[f1][f2] + binpow(V,i+1)) % mod;
                }
            }
        }

        int f1 = 1, f2 = 1;
        for (int i = 0; i < q; ++i) {
            int t;
            cin >> t;

            if (t == 1)f1 ^= 1;
            if (t == 2)f2 ^= 1;
            cout << cost[f1][f2] << "\n";
        }
    } else {
        va[cost] = a;

        for (auto t: ques) {
            if (move[cost].empty())move[cost].resize(4,-1);
            if (move[cost][t] != -1){
                cost = move[cost][t];
            } else {
                a = va[cost];
                vector<ll> na(n);
                if (t == 1) {
                    for (int j = 0; j < n; ++j) {
                        na[n - j - 1] = a[j];
                    }
                } else if (t == 2) {
                    for (int j = 0; j < n; ++j) {
                        na[j] = n - a[j] + 1;
                    }
                } else if (t == 3) {
                    for (int j = 0; j < n; ++j) {
                        na[a[j] - 1] = j + 1;
                    }
                }

                ll cost2 = 0;
                for (int j = 0; j < n; ++j) {
                    cost2 = (cost2 + binpow(na[j], j + 1)) % mod;
                }
                va[cost2] = na;
                move[cost][t] = cost2;
                cost = cost2;
            }

            cout << cost << "\n";
        }
    }

    
}