#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using ll = long long;
#define int long long
template<typename T>
using ordered_set = tree<T, null_type, std::less<T>, rb_tree_tag, tree_order_statistics_node_update>;
using namespace std;

template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &a) {
    return in >> a.first >> a.second;
}

template<typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &a) {
    return out << a.first << ' ' << a.second << '\n';
}

template<typename T>
istream &operator>>(istream &in, vector<T> &a) {
    for (auto &i: a)in >> i;
    return in;
}

template<typename T>
ostream &operator<<(ostream &out, const vector<T> &a) {
    for (auto &i: a)out << i << ' ';
    return out;
}

template<typename T>
ostream &operator<<(ostream &out, const vector<vector<T> > &a) {
    for (auto &i: a)out << i << '\n';
    return out;
}

constexpr int mod = 998244353;

template<typename T>
T fast_pow(T a, int n) {
    if (n == 0)return 1;
    if (n % 2 == 1)return a * fast_pow(a, n - 1);
    T t = fast_pow(a, n / 2);
    return t * t;
}

struct Zm {
    int k;

    Zm(int k = {}) : k(k % mod) {
        if (this->k < 0)this->k += mod;
    }

    Zm operator+(const Zm &other) const {
        return {k + other.k};
    }

    Zm operator-(const Zm &other) const {
        return {k - other.k};
    }

    Zm operator*(const Zm &other) const {
        return {k * other.k};
    }
};

struct Test {
    int n{};
    vector<int> a;
    vector<int> now = {};
    map<vector<int>, Zm> mp;

    Zm calc() {
        if (!mp.contains(now)) {
            vector<int> p = a;
            for (auto x: now) {
                vector<int> q(n);
                if (x == 1) {
                    for (int i = 0; i < n; i++) {
                        q[i] = p[n - i - 1];
                    }
                } else if (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]] = i;
                    }
                }
                p = q;
            }
            Zm ans = 0;
            for (int i = 0; i < n; i++) {
                ans = ans + fast_pow(Zm(p[i] + 1), i + 1);
            }
            mp[now]=ans;
        }
        return mp[now];
    }

    void optimize(int x0) {
        bool ok = true;
        now.push_back(x0);
        while (ok) {
            ok = false;
            if (now.size() <= 1) {
                continue;
            }
            if (now.size() >= 2) {
                int &x = now[now.size() - 2];
                int &y = now[now.size() - 1];
                if (x == y) {
                    ok = true;
                    now.pop_back();
                    now.pop_back();
                    continue;
                }
                if (x > y) {
                    swap(x, y);
                    if (y == 3)x = 3 - x;
                    ok = true;
                    continue;
                }
            }
            if (now.size()>=3) {
                int &x = now[now.size() - 3];
                int &y = now[now.size() - 2];
                int &z = now[now.size() - 1];
                if (x == y) {
                    ok = true;
                    int z0=z;
                    now.pop_back();
                    now.pop_back();
                    now.pop_back();
                    now.push_back(z0);
                    continue;
                }
                if (x > y) {
                    swap(x, y);
                    if (y == 3)x = 3 - x;
                    ok = true;
                    continue;
                }
            }
        }
    }

    Test() {
        int q;
        cin >> n >> q;
        a.resize(n);
        cin >> a;
        for (auto &i: a)i--;
        while (q--) {
            int x;
            cin >> x;
            optimize(x);
            cout << calc().k << ' ';
        }
    }
};

signed main() {
#ifdef home
    freopen(".in", "r",stdin);
    freopen(".out", "w",stdout);
#else
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#endif
    int t = 1;
    //cin >> t;
    while (t--) {
        Test();
    }
}
