#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <cstdlib>
#include <cstdio>
#include <bitset>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <cmath>

const long long MOD = 998244353ll;

long long bpow(long long a, long long n) {
    long long cur = 1ll;
    while (n != 0) {
        if (n % 2ll == 1ll) {
            cur *= a;
            cur %= MOD;
        }
        a *= a;
        a %= MOD;
        n >>= 1ll;
    }
    return cur;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n, q;
    std::cin >> n >> q;
    std::vector<long long> arr(n);
    for (long long& elem : arr) {
        std::cin >> elem;
    }
    std::vector<int> que(q);
    for (int& elem : que) {
        std::cin >> elem;
    }
    for (int qi : que) {
        std::vector<long long> new_arr(n);
        if (qi == 1) {
            for (int i = 0; i < n; ++i) {
                new_arr[i] = arr[n - (i + 1)];
            }
        } else if (qi == 2) {
            for (int i = 0; i < n; ++i) {
                new_arr[i] = n - arr[i] + 1;
            }
        } else {
            for (int i = 0; i < n; ++i) {
                new_arr[arr[i] - 1] = i + 1;
            }
        }
        long long cost = 0;
        for (int i = 0; i < n; ++i) {
            arr[i] = new_arr[i];
            cost = (cost + bpow(arr[i], i + 1)) % MOD;
        }
        std::cout << cost << ' ';
    }
    std::cout << '\n';
    return 0;
}