#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2,popcnt")
#pragma GCC target("tune=native")

using namespace std;

#define int long long
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define vi vector<int>
#define vvi vector<vector<int>>
#define vpi vector<pair<int>>
#define pi pair<int, int>
#define vvpi vector<vector<pair<int, int>> >
#define sz(a) (int)a.size()

int mod1 = 2286661337;
int mod2 = 91138233;
int mod3 = 97266353;

struct fenwick {
    int n;
    vi f;
    explicit fenwick(int &n): n(n) {
        f.resize(n, 0);
    }

    void upd(int x, int val) {
        for (int i = x; i <= n; i += i & -i) {
            f[i] += val;
        }
    }

    int pref(int x) {
        int sum = 0;
        for (int i = x; i >= 0; i -= i & -i) {
            sum += f[i];
        }
        return sum;
    }

    int sum(int l, int r) {
        return pref(r) - pref(l-1);
    }
};

struct sparse {
    int n, LOG;
    vi lg;
    vvi s;

    explicit sparse(vi a) : n(sz(a)) {
        LOG = ceil(log2(n+1));
        lg.resize(n+1);
        s.resize(LOG, vi(n+1, INT_MAX));

        for (int i = 2; i <= n; i++) lg[i] = lg[i/2] + 1;
    }
};

void solve_a() {
    int n, q; cin >> n >> q;
    vi a(n);
    for (int&i : a) cin >> i;
    while (q--) {
        int t; cin >> t;
        if (t == 1) {
            int x; cin >> x;
        } else {
            int y; cin >> y;

        }
    }
}

int mod = 998244353;

int mod_pow(int a, int m) {
    int res = 1;
    while (m > 0) {
        if (m&1) {
            res = res*a%mod;
            m--;
        }
        a = a*a%mod;
        m/=2;
    }
    return res;
}
void solve_b3() {
    int n, q; cin >> n >> q;
    vi a(n);
    for (int&i : a) cin >> i;
    vi b(q);
    for (int&i : b) cin >> i;
    vi a1(n);
    int ans1 = 0, ans2 = 0;
    for (int i = 0; i < n; i++) {
        a1[i] = n-a[i]+1;
        ans1 = (ans1 + mod_pow(a1[i], i+1))%mod;
    }
    for (int i = 0; i < n; i++) {
        ans2 = (ans2 + mod_pow(a[i], i+1))%mod;
    }
    int last = ans2;
    for (int i = 0; i < q; i++) {
       if (b[i] == 3) cout << last << " ";
        else if (last == ans1) {
            cout << ans2 << " ";
            last = ans2;
        } else {
            cout << ans1  << " ";
            last = ans1;
        }
    }
}

void solve_b2() {
    int n, q; cin >> n >> q;
    vi a(n);
    for (int&i : a) cin >> i;
    vi b(q);
    for (int&i : b) cin >> i;
    for (int i = 0; i < q; i++) {
        int ans1 = 0;
        if (b[i] == 1) {
            reverse(a.begin(), a.end());
            for (int j = 0; j < n; j++) {
               // cout << a[j] << " ";
                ans1 = (ans1 + mod_pow(a[j], j+1))%mod;
            }
        } else if (b[i] == 2) {
            vi a1(n);
            for (int j = 0; j < n; j++) {
                a1[j] = n-a[j]+1;
                // cout << a1[j] << " ";
                ans1 = (ans1 + mod_pow(a1[j], j+1))%mod;
            }
            a = a1;
        } else {
            vi p(n);
            vi a1 = a;
            for (int j = 0; j < n; j++) {
                p[a[j]-1] = j+1;
            }
            for (int j = 0; j < n; j++) {
                a[j] = p[j];
               // cout << a[j] << " ";
                ans1 = (ans1 + mod_pow(a[j], j+1))%mod;
            }
        }
       // cout << endl;
        cout << ans1 << " ";
    }
}

void solve_b1() {
    int n, q; cin >> n >> q;
    vi a(n);
    for (int&i : a) cin >> i;
    vi b(q);
    for (int&i : b) cin >> i;
    int ans1 = 0, ans2 = 0;
    if (b[0] == 2) {
        vi a1(n);
        for (int i = 0; i < n; i++) {
            a1[i] = n-a[i]+1;
           // cout << a1[i] << " " << endl;
            ans1 = (ans1 + mod_pow(a1[i], i+1))%mod;
        }
        for (int i = 0; i < n; i++) {
            ans2 = (ans2 + mod_pow(a[i], i+1))%mod;
        }
    } else if (b[0] == 1) {
        reverse(a.begin(), a.end());
        for (int i = 0; i < n; i++) {
            ans1 = (ans1 + mod_pow(a[i], i+1))%mod;
        }
        reverse(a.begin(), a.end());
        for (int i = 0; i < n; i++) {
            ans2 = (ans2 + mod_pow(a[i], i+1))%mod;
        }
    } else {
        vi p(n);
        vi a1 = a;
        for (int i = 0; i < n; i++) {
            p[a[i]-1] = i+1;
        }
        for (int i = 0; i < n; i++) {
            a[i] = p[i];
            ans1 = (ans1 + mod_pow(a[i], i+1))%mod;
        }
        for (int i = 0; i < n; i++) {
            ans2 = (ans2 + mod_pow(a1[i], i+1))%mod;
        }
    }
    for (int i = 0; i < q; i++) {
        if (i&1) {
            cout << ans2 <<" ";
        } else {
            cout << ans1 << " ";
        }
    }
}

void solve_d() {
    int n; cin >> n;
    vi a(n), b(n), p(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
    }
    for (int&i : p) cin >> i;
    int cur = 0;
    vi t(n, -1);
    int cr = 0;
    int k = 0;
    for (int i = 0; i < n; i++) {
        if (p[i] != -1) {
            p[i] -= cr;
            for (int j = k; j < p[i]+k; j++) {t[j] = i;}
            k = p[i] + k;
            cr += p[i];
        }
    }
    //for (int&i : t) cout << i << " ";
    //cout << endl;
    int mx;
    for (int i = 0; i < n; i++) {
        if (t[i] == -1) continue;
        cur += a[i] + t[i]*b[i];
    }
    mx = cur;
    for (int i = 0; i < n; i++) {
        if (t[i] != -1) continue;
        cur += a[i] + b[i]*i;
        mx = max(cur, mx);
    }
    cout << mx;
}

void solve_h() {
    int n, q; cin >> n >> q;
    vi a(n);
    for (int &i : a) cin >> i;
    for (int i = 0; i < q; i++) {
        int t;
        if (t == 1) {
            int x; cin >> x;
        } else {

        }
    }
}

void solve_b() {
    int n, q; cin >> n >> q;
    vi a(n);
    for (int&i : a) cin >> i;
    vi b(q);
    for (int&i : b) cin >> i;
    vi ans(4);
    vi b1 = b;
    b.clear();
    b.resize(4);
    b = {1, 2, 1, 2};
    for (int i = 0; i < 4; i++) {
        int ans1 = 0;
        if (b[i] == 1) {
            reverse(a.begin(), a.end());
            for (int j = 0; j < n; j++) {
                ans1 = (ans1 + mod_pow(a[j], j+1))%mod;
            }
        } else if (b[i] == 2) {
            vi a1(n);
            for (int j = 0; j < n; j++) {
                a1[j] = n-a[j]+1;
                ans1 = (ans1 + mod_pow(a1[j], j+1))%mod;
            }
            a = a1;
        } else {
            vi p(n);
            vi a1 = a;
            for (int j = 0; j < n; j++) {
                p[a[j]-1] = j+1;
            }
            for (int j = 0; j < n; j++) {
                a[j] = p[j];
                ans1 = (ans1 + mod_pow(a[j], j+1))%mod;
            }
        }
        ans[i] = ans1;
    }
    swap(b1, b);
    vi res(q), sos(q);
    int sost = (b[0] == 1 ? 1 : 3);
    res[0] = ans[sost-1];
    sos[0] = sost;
    for (int i = 1; i < q; i++) {
        if (i > 1 && b[i] == b[i-1]) {
            sos[i] = sos[i-2];
            sost = sos[i-2];
            res[i] = res[i-2];
            continue;
        }
        if (sost == 1) {
            if (b[i] == 1) {
                sost = 4;
            } else {
                sost = 2;
            }
        } else if (sost == 2) {
            if (b[i] == 1) {
                sost = 3;
            } else {
                sost = 1;
            }
        } else if (sost == 3) {
            if (b[i] == 1) {
                sost = 2;
            } else {
                sost = 4;
            }
        } else {
            if (b[i] == 1) {
                sost = 1;
            } else {
                sost = 3;
            }
        }
        sos[i] = sost;
        res[i] = ans[sost-1];
    }
   for (int i = 0; i < q; i++) {
       cout << res[sos[i]-1] << " ";
   }
}


signed main() {
    int t = 1;
    //cin >> t;
    while (t--) {
        solve_b();
    }
}

/*
5 10
3 5 4 2 1
1 2 1 2 1 2 2 2 2 1
*/