#include "bits/stdc++.h"

using namespace std;

template<typename T>
istream &operator>>(istream &is, vector<T> &a) {
      for (T &elem: a) is >> elem;
      return is;
}

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

#define int long long

const int mod = 998244353;

int binpow(int a, int n) {
      int ans = 1;
      while (n > 0) {
            if (n & 1) ans = ans * a % mod;
            a = a * a % mod;
            n /= 2;
      }
      return ans;
}

signed main() {
#ifdef LOCAL
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
#else
      cin.tie(false);
      ios_base::sync_with_stdio(false);
#endif
      int n, q1;
      cin >> n >> q1;
      vector<int> a(n + 1);
      for (int i = 1; i <= n; ++i) cin >> a[i];

      map<vector<int>, int> used;
      queue<vector<int>> q;
      q.push(a);

      auto op1 = [&](const vector<int> &x) {
            vector<int> res(x.size());
            for (int i = 1; i <= n; ++i) res[i] = x[n - i + 1];
            return res;
      };
      auto op2 = [&](const vector<int> &x) {
            vector<int> res(x.size());
            for (int i = 1; i <= n; ++i) res[i] = n - x[i] + 1;
            return res;
      };
      auto op3 = [&](const vector<int> &x) {
            vector<int> res(x.size());
            for (int i = 1; i <= n; ++i) res[x[i]] = i;
            return res;
      };

      auto calc = [&](const vector<int> &x) {
            int res = 0;
            for (int i = 1; i <= n; ++i) {
                  res = (res + binpow(x[i], i));
                  if (res >= mod) res -= mod;
            }
            return res;
      };
      while (!q.empty()) {
            auto now = q.front();
            q.pop();

            auto n1 = op1(now);
            auto n2 = op2(now);
            auto n3 = op3(now);

            if (!used.count(n1)) {
                  used[n1] = calc(n1);
                  q.push(n1);
            }
            if (!used.count(n2)) {
                  used[n2] = calc(n2);
                  q.push(n2);
            }
            if (!used.count(n3)) {
                  used[n3] = calc(n3);
                  q.push(n3);
            }
      }

//      cout << used.size() << '\n';
      while (q1--) {
            int op;
            cin >> op;
            if (op == 1) {
                  a = op1(a);
            } else if (op == 2) {
                  a = op2(a);
            } else {
                  a = op3(a);
            }
            cout << used[a] << ' ';
      }
}
