#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <experimental/random>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ordered_set = tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>;
using str = string;
using ld = long double;
const ll INF = 1e18, MOD = 998244353;
#define int long long
int binp(int v,int x) {
    if (x == 0) {
        return 1;
    }
    auto w = binp(v, x / 2);
    if (x % 2) {
        return w * w % MOD * v % MOD;
    }
    return w * w % MOD;
}

void solve();

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int _ = 1;
    //cin >> _;
    while (_--) {
        solve();
        //cout << "\n";
    }
}

int calc(vector<int> p) {
    int ans = 0;
    for (int i = 0; i < p.size(); i++) {
        ans += binp(p[i], i + 1);
        ans %= MOD;
    }
    return ans;
}

void solve() {
    int n, q;
    cin >> n >> q;
    vector<vector<vector<int> > > t(2, vector<vector<int> >(4, vector<int>(n)));
    for (int i = 0; i < n; ++i) {
        cin >> t[0][0][i];
        t[1][0][t[0][0][i] - 1] = i + 1;
    }
    for (int i = 0; i < n; ++i) {
        t[0][1][i] = t[0][0][n - 1 - i];
        t[0][3][i] = n + 1 - t[0][0][n - 1 - i];
        t[0][2][i] = n + 1 - t[0][0][i];
        t[1][1][i] = t[1][0][n - 1 - i];
        t[1][3][i] = n + 1 - t[1][0][n - 1 - i];
        t[1][2][i] = n + 1 - t[1][0][i];
    }
    vector<vector<int> > ans(2, vector<int>(4));
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 4; ++j) {
            ans[i][j] = calc(t[i][j]);
        }
    }
    while (q--) {
        int b;
        cin >> b;
        if (b == 1) {
            swap(t[0][0], t[0][1]);
            swap(t[0][2], t[0][3]);
            swap(t[1][0], t[1][2]);
            swap(t[1][1], t[1][3]);
            swap(ans[0][0], ans[0][1]);
            swap(ans[0][2], ans[0][3]);
            swap(ans[1][0], ans[1][2]);
            swap(ans[1][1], ans[1][3]);
        } else if (b == 2) {
            swap(t[0][0], t[0][2]);
            swap(t[0][1], t[0][3]);
            swap(t[1][0], t[1][1]);
            swap(t[1][2], t[1][3]);
            swap(ans[0][0], ans[0][2]);
            swap(ans[0][1], ans[0][3]);
            swap(ans[1][0], ans[1][1]);
            swap(ans[1][2], ans[1][3]);
        } else if (b == 3) {
            swap(t[0], t[1]);
            swap(ans[0], ans[1]);
        }
        cout << ans[0][0] << "\n";
    }
}
