#include <iostream>
#include <vector>
#include <unordered_map>
#include <cmath>

using namespace std;
typedef long long ll;


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

    ll n, q;

    cin >> n >> q;

    vector<ll> p(n+1);

    for (ll i = 1; i <= n; i++) {
        cin >> p[i];
    }

    vector<unordered_map<ll, ll>> mem(n+1);
    for (ll i = 1; i <= n; i++) {
        mem[i] = unordered_map<ll, ll>();
    }

    for (ll r = 0; r < q; r++) {
        ll request;
        cin >> request;
        vector<ll> new_p(n+1);
        if (request == 1) {
            for (ll i = 1; i <= n; i++) {
                new_p[i] = p[n-i+1];
            }
        }
        else if (request == 2) {
            for (ll i = 1; i <= n; i++) {
                new_p[i] = n - p[i] + 1;
            }
        }
        else {
            for (ll i = 1; i <= n; i++) {
                new_p[p[i]] = i;
            }

        }

        ll price = 0;
        for (ll i = 1; i <= n; i++) {
            ll current_element = new_p[i];
            if (!mem[i].contains(new_p[i])) {
                mem[i][current_element] = static_cast<ll>(pow(current_element, i)) % 998244353;
            }
            price += mem[i][current_element];
         }

        cout << price << " ";
        p = new_p;
    }


    return 0;
}