#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <random>

using namespace std;

const int64_t mod = 998'244'353;

int64_t binPow(int a, int b) {
    if (b == 0) return 1;
    int64_t x = binPow(a, b / 2);
    x = (x * x) % mod;
    if (b % 2) x *= a;
    return x % mod;
}

int main()
{
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, q;
    cin >> n >> q;
    vector <int64_t> p(n);
    vector <int64_t> pw(n);
    int64_t cost0 = 0;
    int64_t cost1 = 0;
    int64_t cost2 = 0;
    int64_t cost3 = 0;
    int64_t cost4 = 0;
    int64_t cost5 = 0;
    int64_t cost6 = 0;
    int64_t cost7 = 0;


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

    for (int i = 0; i < n; ++i) {
        int k1 = p[i];
        int k2 = n - p[i] + 1;
        int c1 = i + 1;
        int c2 = n - i;

        cost0 += binPow(k1, c1);
        cost1 += binPow(c1, k1);

        cost2 += binPow(k1, c2);
        cost3 += binPow(c2, k1);

        cost4 += binPow(k2, c1);
        cost5 += binPow(c1, k2);

        cost6 += binPow(k2, c2);
        cost7 += binPow(c2, k2);

        cost0 %= mod;
        cost1 %= mod;
        cost2 %= mod;
        cost3 %= mod;
        cost4 %= mod;
        cost5 %= mod;
        cost6 %= mod;
        cost7 %= mod;
    }

  //  cout << cost0 << " " << cost1 << " " << cost2 << " " << cost3 << " " << cost4 << " " << cost5 << " " << cost6 << " " << cost7 << "\n";

    int64_t c0 = 0;
    int64_t c1 = 0;
    int64_t c2 = 0;
    int64_t c3 = 0;
    int64_t c4 = 0;
    int64_t c5 = 0;
    int64_t c6 = 0;
    int64_t c7 = 0;
    for (int i = 0; i < n; ++i) {
        int k1 = p[i];
        int k2 = n - p[i] + 1;
        int x1 = pw[i];
        int x2 = n - pw[i] + 1;

        c0 += binPow(k1, x1);
        c1 += binPow(x1, k1);

        c2 += binPow(k1, x2);
        c3 += binPow(x2, k1);

        c4 += binPow(k2, x1);
        c5 += binPow(x1, k2);

        c6 += binPow(k2, x2);
        c7 += binPow(x2, k2);
    }
  //  cout << c0 << " " << c1 << " " << c2 << " " << c3 << " " << c4 << " " << c5 << " " << c6 << " " << c7 << "\n\n";

    for (int i = 0; i < q; ++i) {
        int b;
        cin >> b;
        int64_t ans = 0;
        if (b == 1) {
            swap(cost2, cost0);
            swap(cost1, cost5);
            swap(cost3, cost7);
            swap(cost4, cost6);
            for (int i = 0; i < n; ++i) {
                pw[i] = n - pw[i] + 1;
            }
        } else if (b == 2) {
            swap(cost0, cost4);
            swap(cost1, cost3);
            swap(cost5, cost7);
            swap(cost2, cost6);
            for (int i = 0; i < n; ++i) {
                p[i] = n - p[i] + 1;
            }
        } else if (b == 3) {
            swap(cost0, cost1);
            swap(cost2, cost3);
            swap(cost4, cost5);
            swap(cost6, cost7);
            for (int i = 0; i < n; ++i) {
                swap(p[i], pw[i]);
            }
        }

       // cout << cost0 << " " << cost1 << " " << cost2 << " " << cost3 << " " << cost4 << " " << cost5 << " " << cost6 << " " << cost7 << "\n";

        int64_t c0 = 0;
        int64_t c1 = 0;
        int64_t c2 = 0;
        int64_t c3 = 0;
        int64_t c4 = 0;
        int64_t c5 = 0;
        int64_t c6 = 0;
        int64_t c7 = 0;
        for (int i = 0; i < n; ++i) {
            int k1 = p[i];
            int k2 = n - p[i] + 1;
            int x1 = pw[i];
            int x2 = n - pw[i] + 1;

            c0 += binPow(k1, x1);
            c1 += binPow(x1, k1);

            c2 += binPow(k1, x2);
            c3 += binPow(x2, k1);

            c4 += binPow(k2, x1);
            c5 += binPow(x1, k2);

            c6 += binPow(k2, x2);
            c7 += binPow(x2, k2);
        }
        //cout << c0 << " " << c1 << " " << c2 << " " << c3 << " " << c4 << " " << c5 << " " << c6 << " " << c7 << "\n\n";
        cout << c1 << " ";
    }


    return 0;
}
