#include <bits/stdc++.h>

#define all(v) v.begin(), v.end()
#define len(v) (int)v.size()

using namespace std;
using ll = long long;
using ld = long double;

const ll Mod = 998'244'353;

ll pw(ll a, ll b)
{
    if (b == 0) return 1;
    if (b % 2 == 1)
    {
        return (a * pw(a, b - 1)) % Mod;
    }
    ll x = pw(a, b / 2);
    return (x * x) % Mod;
}

void solve()
{
    int n, q;
    cin >> n >> q;
    vector<int> a(n);
    for (int i = 0; i < n; ++i)
    {
        cin >> a[i];
    }
    vector<vector<vector<ll>>> ans(2, vector<vector<ll>>(2, vector<ll>(2, 0)));
    for (int r = 0; r < 2; ++r)
    {
        for (int c = 0; c < 2; ++c)
        {
            for (int ob = 0; ob < 2; ++ob)
            {
                auto b = a;
                if (ob)
                {
                    auto c = b;
                    for (int i = 0; i < n; ++i)
                    {
                        c[b[i] - 1] = i + 1;
                    }
                    b = c;
                }
                if (r)
                {
                    reverse(all(b));
                }
                if (c)
                {
                    for (int i = 0; i < n; ++i)
                    {
                        b[i] = n + 1 - b[i];
                    }
                }
                for (int i = 0; i < n; ++i)
                {
                    ans[r][c][ob] = (ans[r][c][ob] + pw(b[i], i + 1)) % Mod;
                }
            }
        }
    }
    int r = 0, c = 0, ob = 0;
    for (int i = 0; i < q; ++i)
    {
        int t;
        cin >> t;
        if (t == 1)
        {
            r ^= 1;
        } else if (t == 2)
        {
            c ^= 1;
        } else
        {
            ob ^= 1;
            swap(r, c);
        }
        cout << ans[r][c][ob] << ' ';
    }
}

int main()
{
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);

    //cin >> t;

    while (t--)
    {
        solve();
        cout << '\n';
    }
    return 0;
}
