#ifdef LOCAL
#define GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

constexpr ll mod = 998244353;

mt19937_64 rnd(28);

ll add(ll a, ll b) {
    if (a + b >= mod) {
        return a + b - mod;
    }
    return a + b;
}

ll sub(ll a, ll b) {
    if (a - b < 0) {
        return a - b + mod;
    }
    return a - b;
}

ll mult(ll a, ll b) {
    return __int128(a) * b % mod;
}

ll binp(ll a, ll x) {
    ll ans = 1;
    while (x > 0) {
        if (x & 1) {
            ans = ans * a % mod;
        }
        a = a * a % mod;
        x /= 2;
    }
    return ans;
}

ll divd(ll a, ll b) {
    return mult(a, binp(b, mod - 2));
}

void solve() {
    ll n, m;
    cin >> n >> m;
    vector<ll> p(n), b(m);
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
    }
    for (int i = 0; i < m; ++i) {
        cin >> b[i];
    }
    for (int x = 0; x < m; ++x) {
        vector<ll> q(n);
        if (b[x] == 1) {
            for (int i = 0; i < n; ++i) {
                q[i] = p[n - i - 1];
            }
        } else if (b[x] == 2) {
            for (int i = 0; i < n; ++i) {
                q[i] = n - p[i] + 1;
            }
        } else {
            for (int i = 0; i < n; ++i) {
                q[p[i] - 1] = i + 1;
            }
        }
        p = q;
        ll ans = 0;
        for (int i = 0; i < n; ++i) {
            ans = add(ans, binp(p[i], i + 1));
        }
        cout << ans << ' ';
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ll tt = 1;
    //cin >> tt;
    while (tt--) {
        solve();
        cout << endl;
    }
}