#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];
    }
    vector<ll> sp = p;
    ll ans1 = 0, ans2 = 0, ans3 = 0, ans4 = 0;
    for (int i = 0; i < n; ++i) {
        ans1 = add(ans1, binp(p[i], i + 1));
    }
    vector<ll> q(n);
    for (int i = 0; i < n; ++i) {
        q[i] = p[n - i - 1];
    }
    p = q;
    for (int i = 0; i < n; ++i) {
        ans2 = add(ans2, binp(p[i], i + 1));
    }
    p = sp;
    for (int i = 0; i < n; ++i) {
        q[i] = n - p[i] + 1;
    }
    p = q;
    for (int i = 0; i < n; ++i) {
        ans3 = add(ans3, binp(p[i], i + 1));
    }
    for (int i = 0; i < n; ++i) {
        q[i] = p[n - i - 1];
    }
    p = q;
    for (int i = 0; i < n; ++i) {
        ans4 = add(ans4, binp(p[i], i + 1));
    }
    //cout << ans1 << ' ' << ans2 << ' ' << ans3 << ' ' << ans4 << '\n';
    ll cnt1 = 0, cnt2 = 0;
    for (int i = 0; i < m; ++i) {
        cnt1 += bool(b[i] == 1);
        cnt2 += bool(b[i] == 2);
        if (cnt1 % 2 == 0 && cnt2 % 2 == 0) {
            cout << ans1 << '\n';
        }
        if (cnt1 % 2 == 1 && cnt2 % 2 == 0) {
            cout << ans2 << '\n';
        }
        if (cnt1 % 2 == 0 && cnt2 % 2 == 1) {
            cout << ans3 << '\n';
        }
        if (cnt1 % 2 == 1 && cnt2 % 2 == 1) {
            cout << ans4 << '\n';
        }
    }
}

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;
    }
}