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

using namespace std;
typedef unsigned long long ll;



ll pow(ll n, ll p) {
    ll result = 1;
    for (ll i = 0; i < p; i++) {
        result *= n;
        result %= 998244353;
    }

    return result;
}

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>();
    }
    vector<ll> result(q);
    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] = pow(current_element, i);
            }
            price += mem[i][current_element];
         }

        result[r] = price;
        p = new_p;
    }


    for (ll i = 0; i < q; i++) {
        cout << result[i] << " ";
    }
    return 0;
}