#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define all(x) x.begin(), x.end()

template<typename T>
int isz(const T& v) {
  return (int)v.size();
}

template<typename T>
void reuniq(T& v) {
  v.erase(unique(all(v)), v.end());
}

template<typename X, typename Y>
bool remin(X& x, const Y& y) {
  if (y < x) {
    x = y;
    return true;
  }
  return false;
}

template<typename X, typename Y>
bool remax(X& x, const Y& y) {
  if (y > x) {
    x = y;
    return true;
  }
  return false;
}

constexpr int kMod = 998'244'353;

vector<int> op1(vector<int> a) {
  reverse(all(a));
  return a;
}

vector<int> op2(vector<int> a) {
  const int n = isz(a);
  for (auto& it : a) {
    it = n - it + 1;
  }
  return a;
}

vector<int> op3(vector<int> a) {
  const int n = isz(a);
  vector<int> q(n);
  for (int i = 0; i < n; ++i) {
    q[a[i] - 1] = i + 1;
  }
  return q;
}

int add(int a, int b) {
  a += b;
  if (a >= kMod) {
    a -= kMod;
  }
  return a;
}

int sub(int a, int b) {
  a -= b;
  if (a < 0) {
    a += kMod;
  }
  return a;
}

int mul(int a, int b) {
  return ll(a) * b % kMod;
}

int binpow(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1) {
      ans = mul(ans, a);
    }
    a = mul(a, a);
    b >>= 1;
  }
  return ans;
}

int calc(const vector<int>& p) {
  const int n = isz(p);
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    ans = add(ans, binpow(p[i], i + 1));
  }
  return ans;
}

void compute(int test_case) {
  int n, m;
  cin >> n >> m;
  vector<int> p(n);
  for (auto& it : p) {
    cin >> it;
  }
  map<vector<int>, int> num;
  map<int, map<int, int>> g;
  map<int, int> val;
  int ptr = 1;
  num[p] = ptr++;
  queue<vector<int>> q;
  q.push(p);
  while (!q.empty()) {
    auto v = q.front();
    q.pop();
    const int i = num[v];
    val[i] = calc(v);
    auto v1 = op1(v);
    if (num.find(v1) == num.end()) {
      num[v1] = ptr++;
      q.push(v1);
    }
    auto v2 = op2(v);
    if (num.find(v2) == num.end()) {
      num[v2] = ptr++;
      q.push(v2);
    }
    auto v3 = op3(v);
    if (num.find(v3) == num.end()) {
      num[v3] = ptr++;
      q.push(v3);
    }
    g[i][1] = num[v1];
    g[i][2] = num[v2];
    g[i][3] = num[v3];
  }
  int u = 1;
  for (int tt = 0; tt < m; ++tt) {
    int b;
    cin >> b;
    u = g[u][b];
    cout << val[u] << " ";
  }
  cout << "\n";
}

signed main() {
  #ifdef LOCAL
    freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
  #endif
  cin.tie(0)->sync_with_stdio(0);
  int t = 1;
  // cin >> t;
  for (int tt = 1; tt <= t; ++tt) {
    compute(tt);
  }
  return 0;
}