#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("avx2")
template<typename T>
istream& operator>>(istream& stream, vector<T>& a) {
    for (auto& x : a) stream >> x;
    return stream;
}
template<typename T>
ostream& operator<<(ostream& stream, vector<T>& a) {
    for (auto& x : a) stream << x << ' ';
    cout << '\n';
    return stream;
}
template<typename T1, typename T2>
istream& operator>>(istream& stream, vector<pair<T1, T2>>& a) {
    for (auto& [x, y] : a) stream >> x >>  y;
    return stream;
}
template<typename T1, typename T2>
ostream& operator<<(ostream& stream, vector<pair<T1, T2>>& a) {
    for (auto& [x, y] : a) stream << x << ' ' << y << '\n';
    return stream;
}
template<typename T1, typename T2>
inline void amax(T1& a, T2 b) {
    a = max(a, b);
}
template<typename T1, typename T2>
inline void amin(T1& a, T2 b) {
    a = min(a, b);
}

//#define int int64_t
#define all(x) x.begin(), x.end()
#define pb emplace_back
#define ff first
#define ss second
#define sz(x) (int)(x.size())
#define deb cout << "----------------------" << endl
#define MIN(x) *min_element(all(x))
#define MINID(x) min_element(all(x)) - x.begin()
#define MAX(x) *max_element(all(x))
#define MAXID(x) max_element(all(x)) - x.begin()
#define push emplace
#define SUM(x) accumulate(all(x), 0ll)
#define sj(x) sort(all(x)), x.erase(unique(all(x)), x.end())

mt19937 rnd(221);

using ll = long long;
using vi = vector<int>;
using pi = pair<int,int>;
using vvi = vector<vi>;
using vpi = vector<pi>;
using vvpi = vector<vpi>;
using ld = long double;

const int64_t INF = 1e18;
const int INF2 = 1e9;
const int mod = 998244353;
const ld eps = 1e-6;

ll bpow(ll x, int st) {
    if (st == 0) return 1;
    ll res = bpow(x, st/ 2);
    res = res * res; res %= mod;
    if (st % 2 == 1) res *= x;
    res %= mod;
    return res;
}

const int N = 1e5 + 2323;
const int D = 500;

vi slow(int n, int q, vi& p, vi& b) {
    vi p2(n);
    auto calc = [&]() -> int {
        int su = 0;
        for (int i = 0; i < n; i++) {
            su += bpow(p[i], i + 1);
            if (su >= mod) su -= mod;
        }
        return su;
    };
    vi ans;
    set<vi> st;
    for (auto x : b) {
        if (x == 1) {
            reverse(all(p));
        } else if (x == 2) {
            for (int i = 0; i < n; i++) {
                p[i] = n - p[i] + 1;
            }
        } else {
            for (int i = 0; i < n; i++) {
                p2[p[i] - 1] = i + 1;
            }
            p = p2;
        }
        st.insert(p);
        cout << p;
        ans.pb(calc());
        // cout << calc() << ' ';
    }
  //  cout << sz(st) << endl;
 //   assert(sz(st) <= 8);
    return ans;
}

vi fast(int n, int q, vi& p, vi& b) {
    vi p2(n);
    auto calc = [&]() -> int {
        int su = 0;
        for (int i = 0; i < n; i++) {
            su += bpow(p[i], i + 1);
            if (su >= mod) su -= mod;
        }
        return su;
    };

    vi pst = p;
    map<vi, int> mp;
    set<vi> st;
    map<vi, int> nomer;
    vector<pair<vi, vi>> pr;
    vi re(10);
    for (int w = 0; w < 3; w++) {
        for (int j = 0; j < 3; j++) {
            for (int k = 0; k < 3; k++) {
                if (w + j + k > 4) continue;
                vi cu;
                if (w >= 1) cu.pb(1);
                if (w == 2) cu.pb(1);
                if (j >= 1) cu.pb(2);
                if (j == 2) cu.pb(2);
                if (k >= 1) cu.pb(3);
                if (k == 2) cu.pb(3);
                do {
                    for (auto x : cu) {
                        if (x == 1) {
                            reverse(all(p));
                        } else if (x == 2) {
                            for (int i = 0; i < n; i++) {
                                p[i] = n - p[i] + 1;
                            }
                        } else {
                            for (int i = 0; i < n; i++) {
                                p2[p[i] - 1] = i + 1;
                            }
                            p = p2;
                        }
                    }
                    if (nomer.find(p) == nomer.end()) {
                        nomer[p] = sz(nomer) + 1;
                        re[nomer[p]] = calc();
                    }
                    pr.pb(cu, p);
                    p = pst;
                } while (next_permutation(all(cu))) ;
            }
        }
    }
   // cout << sz(st) << endl;
    vvpi adj(9);
    for (auto [f, s] : pr) {
        for (auto [f2, s2] : pr) {
            int v = nomer[s], u = nomer[s2];
            if (u == v) continue;
            //v -> u
            if (sz(f2) - 1 != sz(f)) continue;
            int x = f2.back();
            f2.pop_back();
            if (f != f2) {
                f2.pb(x);
                continue;
            }
            f2.pb(x);
            adj[v].pb(u, x);
        }
    }


    /*for (int i = 1; i <= 8; i++) {
       // cout << i << " : \n";
        sj(adj[i]);
        for (auto [x, y] : adj[i]) {
            cout << "adj[" << i << "].pb(" << x << ", " << y << ");" << endl;
        }
    }*/
    int wh = 1;
    vi ans;
    for (int i = 0; i < q; i++) {
        for (auto [x, y] : adj[wh]) {
            if (y == b[i]) wh = x;
        }
      //  cout << wh << " : " << re[wh] << endl;
     //   cout << re[wh];
        ans.pb(re[wh]);
    }

    return ans;
}

void stress() {
    for (int we = 0; we <= 10000;we++) {
        int n = rnd() % 10 + 1;
        int q = 11;
        vi p(n); iota(all(p), 1); shuffle(all(p), rnd);
        vi b(q);
        for (auto& x : b) {
            x = rnd() % 3 + 1;
        }
        vi p2 = p;
        vi fe = slow(n, q, p, b);
        p = p2;
        vi se = fast(n, q, p, b);
        p = p2;
        if (fe != se) {
            cout << n << " " << q << '\n';
            cout << p2;
            cout << b;
            cout << "need : " << fe;
            cout << "have : " << se;
            deb;
            exit(0);
        }
    }
    deb;
    cout << "GOOOOOOOOOOOOOl" << endl;
    deb;
}

void solve() {
   //  stress();
    int n, q; cin >> n >> q;
    vi p(n); cin >> p;
    vi b(q); cin >> b;
    vi res = fast(n, q, p, b);
    cout << res;
}

int32_t main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    int tt = 1;
    // cin >> tt;
    while (tt--) {
        solve();
    }
}
