#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <set>
#include <queue>

using namespace std;

const long long MOD = 998244353;

int main() {
    int n, q;
    cin >> n >> q;
    vector<long long> v(n), b(q);
    for (int i = 0; i < n; i++) {
        cin >> v[i];
    }
    for (int i = 0; i < q; i++) {
        long long ans = 0;
        cin >> b[i];
        if (b[i] == 1) {
            vector<long long> a(n);
            for (int j = 0; j < n; j++) {
                a[j] = v[n - j - 1];
                long long c = 1;
                for (int k = j; k >= 0; k--) {
                    c *= a[j];
                    c %= MOD;
                }
                ans += c;
                ans %= MOD;
            }
            v = a;
        } else if (b[i] == 2) {
            vector<long long> a(n);
            for (int j = 0; j < n; j++) {
                a[j] = n - v[j] + 1;
                long long c = 1;
                for (int k = j; k >= 0; k--) {
                    c *= a[j];
                    c %= MOD;
                }
                ans += c;
                ans %= MOD;
            }
                v = a;
        } else {
            vector<long long> a(n);
            for (int j = 0; j < n; j++) {
                a[v[j] - 1] = j + 1;
            }
            for (int j = 0; j < n; j++) {
                long long c = 1;
                for (int k = j; k >= 0; k--) {
                    c *= a[j];
                    c %= MOD;
                }
                ans += c;
                ans %= MOD;
            }
            v = a;
        }
        cout << ans % MOD << ' ';
    }
}