#include<bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
#define int long long
#define ss second
#define ff first
#define all(x) x.begin(), x.end()
#define sz(x) (int) x.size()
#define ll long long
using namespace std;
//using namespace __gnu_pbds;
//typedef tree<int, null_type, less<int>, rb_tree_tag,
//tree_order_statistics_node_update> ordered_set;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll_loops")
const ll inf = 1e9 + 10;
const ll mod = 998244353;
const ll maxn = 1e6 + 10;

int n, q;
vector<int> p;
vector<int> b;


int binpw(int x, int pw) {
    if (pw == 0)
        return 1;
    if (pw % 2 == 0) {
        int b = binpw(x, pw / 2);
        return b * b % mod;
    }
    return binpw(x, pw - 1) * x % mod;
}


void solve()
{
    int sm1 = 0, sm2 = 0;
    if (b[0] == 1) {
        for (int i = 0; i < n; i++) {
            sm1 += binpw(p[i], i + 1);
            sm1 %= mod;
            
            sm2 += binpw(p[i], n - i);
            sm2 %= mod;
        }
        for (int i = 0; i < q; i++) {
            if (i % 2 == 0)
                cout << sm2 << " ";
            else
                cout << sm1 << " ";
        }
    }
    else if (b[0] == 2) {
        for (int i = 0; i < n; i++) {
            sm1 += binpw(p[i], i + 1);
            sm1 %= mod;

            sm2 += binpw(n - p[i] + 1, i + 1);
            sm2 %= mod;
        }
        for (int i = 0; i < q; i++) {
            if (i % 2 == 0)
                cout << sm2 << " ";
            else
                cout << sm1 << " ";
        }
    }
    else {
        for (int i = 0; i < n; i++) {
            sm1 += binpw(p[i], i + 1);
            sm1 %= mod;

            sm2 += binpw(i + 1, p[i]);
            sm2 %= mod;
        }
        for (int i = 0; i < q; i++) {
            if (i % 2 == 0)
                cout << sm2 << " ";
            else
                cout << sm1 << " ";
        }
    }
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ::freopen("input.txt", "r", stdin);
    ::freopen("output.txt", "w", stdout);

    int t = 1;
    //cin >> t;
    while (t--)
    {
        cin >> n >> q;
        p.assign(n, 0);
        b.assign(q, 0);
        for (int i = 0; i < n; i++)
            cin >> p[i];
        for (int i = 0; i < q; i++)
            cin >> b[i];
        solve();
    }
}
