#include "bits/extc++.h"

using namespace std;
using namespace __gnu_pbds;

#define int long long
#define ordered_set tree<long long, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;

random_device rd;
mt19937 rnd(rd());

// #pragma GCC optimize("O3", "unorll-loops")
// #pragma GCC target("avx2", "bmi", "bmi2", "popcnt", "lzcnt")

int rand(int l, int r) {
    if (r < l)
        assert(false);
    return uniform_int_distribution<int>(l, r)(rnd);
}

const int mod = 998244353;

int binpow(int a, int n) {
    int res = 1;

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

    return res;
}

void solve() {
    int n, q;
    cin >> n >> q;

    vector<int> s(n);

    for (int i = 0; i < n; i++) {
        cin >> s[i];
    }

    vector<int> nw(n);

    auto func1 = [&] {
        for (int i = 0; i < n; i++) {
            nw[i] = s[n - i - 1];
        }

        s = nw;
    };

    auto func2 = [&] {
        for (int i = 0; i < n; i++) {
            nw[i] = n - s[i] + 1;
        }
        s = nw;
    };

    auto func3 = [&] {
        for (int i = 0; i < n; i++) {
            nw[s[i] - 1] = i + 1;
        }
        s = nw;
    };

    int last1 = -1, last2 = -1;

    while (q--) {
        int com;
        cin >> com;

        if (com == 1) func1();
        else if (com == 2) func2();
        else func3();

        int sum = 0;

        if (last1 == -1 || last2 == -1) {
            for (int i = 0; i < n; i++) {
                sum = (sum + binpow(s[i], i + 1)) % mod;
            }
        } else {
            swap(last1, last2);
        }

        if (last1 != -1 && last2 != -1) {
            cout << last2 << ' ';
            continue;
        }
        cout << sum << ' ';
        last1 = last2;
        last2 = sum; 
    }
    cout << '\n';
}

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

    int akbalin = 1;
    // cin >> akbalin;

    while (akbalin--) solve();
}
