#include "bits/stdc++.h"
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

// using namespace __gnu_pbds;
// template<class T> 
// using ordered_set = <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

using namespace std;

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using ll = long long;
using ld = long double;

#define int long long

const int mod = 998244353;

int sum(int a, int b) {
    if (a + b >= mod) {
        return a + b - mod;
    }
    if (a + b < 0) {
        return a + b + mod;
    }
    return a + b;
}
int mul(int a, int b) {
    return 1ll * a * b % mod;
}

int binpow(int a, int b) {
    int deg = a;
    int res = 1;
    for (int i = 0; i <= 30; ++i) {
        if ((b >> i) & 1) {
            res = mul(res, deg);
        }
        deg = mul(deg, deg);
    }
    return res;
}

int calc(vector<int>& p) {
    int res = 0;
    for (int i = 0; i < p.size(); ++i) {
        res = sum(res, binpow(p[i], i + 1));
    }
    return res;
}

void solve() {
    int n, q;
    cin >> n >> q;
    vector<int> p(n);
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
    }

    int ver = 1;
    vector<pair<vector<int>, int>> vers(100);
    vector<vector<int>> g(100, vector<int> (3, -1)); 
    map<vector<int>, int> qq;
    vers[0].first = p;
    vers[0].second = calc(p);
    int cur_v = 0;
    qq[p] = 0;
    for (int i = 0; i < q; ++i) {
        int b;
        cin >> b;
        b--;
        if (g[cur_v][b] == -1) {
            vector<int> cur_p = vers[cur_v].first;
            int value = 0;
            if (b == 0) {
                reverse(all(cur_p));
            } else if (b == 1) {
                for (int j = 0; j < n; ++j) {
                    cur_p[j] = n - cur_p[j] + 1;
                }
            } else {
                vector<int> np(n);
                for (int j = 0; j < n; ++j) {
                    np[cur_p[j] - 1] = j + 1;
                }
                for (int j = 0; j < n; ++j) {
                    cur_p[j] = np[j];
                }
            }


            value = calc(cur_p);
            if (qq.find(cur_p) != qq.end()) {
                g[cur_v][b] = qq[cur_p];
            } else {
                qq[cur_p] = ver;
                g[cur_v][b] = ver;  
                vers[ver] = {cur_p, value};
                ver++;
            }
            cur_v = g[cur_v][b];
        } else {
            cur_v = g[cur_v][b];
        }
        cout << vers[cur_v].second << '\n';
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    #ifdef LOCAL
    freopen("C:\\Users\\User\\Documents\\olymp\\input.txt", "r", stdin);
    freopen("C:\\Users\\User\\Documents\\olymp\\output.txt", "w", stdout);
    #endif

    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}