#include <bits/stdc++.h>

using namespace std;

#pragma GCC optimise("O3, unroll-loops")
#pragma GCC target("avx,avx2,popcnt")

#define int long long
#define pb push_back
#define all(a) a.begin(), a.end()
#define db long double
#define ff first
#define ss second
#define vi vector<int>
#define pii pair<int, int>
#define vpii vector<pii>
#define vvi vector<vi>

const int N = 1e5;
const int K = 20;
const int INF = 1e9;
const int mod = 998244353;
constexpr char nl = '\n';

// lando norris 2025 world champion

int bin_pow(int a, int p) {
    int res = 1;
    int pw = a;
    while (p) {
        if (p & 1) {
            res = res * pw % mod;
        }
        pw = pw * pw % mod;
        p >>= 1;
    }
    return res % mod;
}

void solve() {
    int n, q;
    cin >> n >> q;
    vi p(n + 1);
    for (int i = 1; i <= n; ++i) {
        cin >> p[i];
    }
    auto cost = [&]() {
        int sum = 0;
        for (int i = 1; i <= n; ++i) {
            // cout << bin_pow(p[i], i) << nl;
            sum += bin_pow(p[i], i);
            sum %= mod;
        }
        return sum;
    };
    vi bb(q);
    bool same = 1;
    for (int i = 0; i < q; ++i) {
        cin >> bb[i];
        if (i) same &= bb[0] == bb[i];
    }
    if (same) {
        int cost1 = cost();
        if (bb[0] == 1) {
            vi p1(n + 1);
            for (int i = 1; i <= n; ++i) {
                p1[i] = p[n - i + 1];
            }
            p = p1;
        } else if (bb[0] == 2) {
            vi p1(n + 1);
            for (int i = 1; i <= n; ++i) {
                p1[i] = n - p[i] + 1;
            }
            p = p1;
        } else {
            vi p1(n + 1);
            for (int i = 1; i <= n; ++i) {
                p1[p[i]] = i;
            }
            p = p1;
        }
        int cost2 = cost();
        for (int i = 0; i < q; ++i) {
            if (i % 2 == 0) {
                cout << cost1 << ' ';
            } else {
                cout << cost2 << ' ';
            }
        }
    }
    // while (q--) {
    //     int b;
    //     cin >> b;
    //     if (b == 1) {
    //         vi p1(n + 1);
    //         for (int i = 1; i <= n; ++i) {
    //             p1[i] = p[n - i + 1];
    //         }
    //         p = p1;
    //     } else if (b == 2) {
    //         vi p1(n + 1);
    //         for (int i = 1; i <= n; ++i) {
    //             p1[i] = n - p[i] + 1;
    //         }
    //         p = p1;
    //     } else {
    //         vi p1(n + 1);
    //         for (int i = 1; i <= n; ++i) {
    //             p1[p[i]] = i;
    //         }
    //         p = p1;
    //     }
    //     cout << cost() << ' ';
    // }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
}
