#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
using uc = unsigned char;
using ull = unsigned long long;
using ul = unsigned long;
using i128 = __int128_t;
using u128 = __uint128_t;

ll MOD = 998244353LL;

inline ll binpow(ll a, ll e) {
    ll res = 1;

    while (e) {
        if (e & 1) res = res * a % MOD;
        a = a * a % MOD;
        e >>= 1;
    }

    return res;
}


inline void solve(ostringstream& os) {
    int n, q;
    cin >> n >> q;

    vector<ll> a(n);
    for (int i = 0; i < n; ++i) cin >> a[i];
    bool rise = true;
    for (int i = 1; i < n; ++i) {
        if (a[i] < a[i - 1]) {
            rise = false;
            break;
        }
    }

    auto print = [&os](vector<ll>& a) -> void {
        for (ll an : a) os << an << ' ';
        os << '\n';
    };

    auto count = [&]() -> ll {
        ll res = 0;
        for (ll i = 0; i < n; ++i) {
            res = (res + binpow(a[i], i + 1)) % MOD;
        }
        return res;
    };

    if (rise) {
        bool state = true;
        vector<ll> ans(2);
        ans[1] = count();
        reverse(a.begin(), a.end());
        ans[0] = count();

        for (int i = 0; i < q; ++i) {
            int type;
            cin >> type;

            if (type == 1) {
                state = !state;
                os << ans[state] << ' ';
            }

            else if (type == 2) {
                state = !state;
                os << ans[state] << ' ';
            }

            else {
                os << ans[state] << ' ';
            }
        }

        return;
    }

    vector<int> queues(q);
    vector<int> cnt(4);
    for (int i = 0; i < q; ++i) {
        int type;
        cin >> type;
        cnt[type]++;

        queues[i] = type;
    }

    int amount = 0;
    int tp = -1;
    for (int i = 1; i < 4; ++i) {
        if (cnt[i] != 0) {
            amount++;
            tp = i;
        }
    }

    bool same = amount == 1;
    if (same) {
        bool state = true;
        vector<ll> ans(2);
        ans[1] = count();

        if (tp == 1) {
            reverse(a.begin(), a.end());
            ans[0] = count();
        }

        else if (tp == 2) {
            for (int j = 0; j < n; ++j) {
                a[j] = n - a[j] + 1;
            }
            ans[0] = count();
        }

        else {
            vector<ll> new_a(n);
            for (int j = 0; j < n; ++j) {
                new_a[a[j] - 1] = j + 1;
            }
            a = new_a;
            ans[0] = count();
        }

        for (int type : queues) {
            state = !state;
            os << ans[state] << ' ';
        }

        return;
    }

    for (int type : queues) {
        if (type == 1) {
            reverse(a.begin(), a.end());
            os << count() << ' ';
        }

        else if (type == 2) {
            for (int j = 0; j < n; ++j) {
                a[j] = n - a[j] + 1;
            }

            os << count() << ' ';
        }

        else {
            vector<ll> new_a(n);
            for (int j = 0; j < n; ++j) {
                new_a[a[j] - 1] = j + 1;
            }
            a = new_a;

            os << count() << ' ';
        }
    }
}


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    ostringstream os;

    solve(os);

    cout << os.str();
    // exit(0);
}