#include <bits/stdc++.h>
using namespace std;

#define int long long
#define ll long long
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define ff first
#define ss second

const int INF = 998244353;

ll powf(ll x, int p) {
    if (p == 0) return 1;
    if (p == 1) return x;
    ll g = powf(x, p / 2);
    g = (g * g) % INF;
    if (p % 2 == 0) return g;
    else return (g * x) % INF;
}

int n;
int get(vector<int> &p) {
    int sm = 0;
    for (int i = 0; i < n; ++i) {
        sm += powf(p[i], i + 1);
        sm %= INF;
    }
    return sm;
}

void solve() {
    int q;
    cin >> n >> q;
    vector<int> p(n);
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
    }

    int b;
    for (int j = 0; j < q; ++j) cin >> b;

    int ans1 = get(p), ans2 = 0;
    if (b == 1) {
        reverse(all(p));
        ans2 = get(p);
    } else if (b == 2) {
        for (int i = 0; i < n; ++i) {
            p[i] = n - p[i] + 1;
        }
        ans2 = get(p);
    } else {
        vector<int> a(n);
        for (int i = 0; i < n; ++i) {
            a[p[i] - 1] = i + 1;
        }
        p = a;
        ans2 = get(p);
    }

    for (int i = 0; i < q; ++i) {
        if (i % 2 == 0) cout << ans2 << ' ';
        else cout << ans1 << ' ';
    }


    /*
    int q;
    cin >> n >> q;
    vector<int> p(n);
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
    }

    vector<int> a(n);
    while (q--) {
        int t;
        cin >> t;
        if (t == 1) {
            reverse(all(p));
        } else if (t == 2) {
            for (int i = 0; i < n; ++i) {
                p[i] = n - p[i] + 1;
            }
        } else {
            for (int i = 0; i < n; ++i) {
                a[p[i] - 1] = i + 1;
            }
            p = a;
        }

        cout << get(p) << ' ';
    }
    */
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int t = 1;
    //cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
