#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(nullptr);
      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);
            }
      }


      vector<function<vector<int> (vector<int>)>> op_ = {op1, op2, op3};

      vector<int> val(8);
      vector<vector<int>> aa(8);
      vector<vector<int>> next(8, vector<int> (3, -1));
      auto make_op = [&](int ops, int op) {
            if (next[ops][op] != -1) return next[ops][op];
            auto t = aa[ops];
            t = op_[op](t);
            next[ops][op] = find(aa.begin(), aa.end(), t) - aa.begin();
            return next[ops][op];
      };
      int ops = 0;
      for (int mask = 0; mask < 8; ++mask) {
            auto now = a;
            for (int i = 0; i < 3; ++i) {
                  if ((mask >> i) & 1) now = op_[i](now);
            }
            val[mask] = calc(now);
            aa[mask] = now;
//            cout << bitset<3>(mask) << ": " << now;
      }
//      {
//            vector<int> now = a;
//            cout <<(op1(op3(now)));
//            cout << op3(op2(now));
//      }
      vector<int> val2 = val;
      val2[6] = val[5];
      val2[5] = val[6];
//      cout << val;


      while (q1--) {
            int op;
            cin >> op;
            op--;
            ops = make_op(ops, op);
            cout << val[ops] << ' ';
      }
}
