#include <bits/stdc++.h>
using namespace std;
template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
  for (auto& it : v) is >> it;
  return is;
}
template <class L, class R>
ostream& operator<<(ostream& os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <class T>
ostream& operator<<(ostream& os, vector<T> V) {
  os << "[ ";
  for (auto v : V) os << v << " ";
  return os << "]";
}
template <class T>
ostream& operator<<(ostream& os, set<T> second) {
  os << "{ ";
  for (auto s : second) os << s << " ";
  return os << "}";
}
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cout << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " |";
  __f(comma + 1, args...);
}
const long long mod = 998244353;
vector<long long> f;
long long n;
long long get(long long pos) {
  long long res = 0;
  for (; pos >= 0; pos = (pos & (pos + 1)) - 1) res += f[pos];
  return res;
}
void update(long long pos, long long val) {
  for (; pos < (long long)(f.size()); pos = (pos | (pos + 1))) f[pos] += val;
}
long long getb(long long ind) {
  long long l = 1, r = n, ans;
  while (l <= r) {
    long long m = (l + r) >> 1;
    long long id = get(m);
    if (id >= ind) {
      r = m - 1;
      if (id == ind) ans = m;
    } else {
      l = m + 1;
    }
  }
  return ans;
}
void solve() {
  long long k;
  cin >> n >> k;
  map<long long, long long> ind, rind;
  f = vector<long long>(n + 5);
  for (long long i = 1; i <= n; ++i) {
    long long x;
    cin >> x;
    ind[x] = i;
    rind[i] = x;
    update(i, 1);
  }
  vector<long long> st(k);
  cin >> st;
  set<long long> rem(st.begin(), st.end());
  long long ans = 1;
  for (long long val : st) {
    long long id = ind[val];
    long long aid = get(id);
    long long lval = -1, rval = -1;
    if (aid > 1) {
      lval = rind[getb(aid - 1)];
    }
    if (aid < n + rem.size() - k) {
      rval = rind[getb(aid + 1)];
    }
    if ((rem.count(rval) or rval == -1) and (rem.count(lval) or lval == -1)) {
      cout << "0\n";
      return;
    }
    if (rem.count(rval) == 0 and rval != -1 and rem.count(lval) == 0 and
        lval != -1) {
      ans = (ans * 2) % mod;
    }
    if (rval != -1 and rem.count(rval) == 0) {
      update(getb(aid + 1), -1);
    } else {
      update(getb(aid - 1), -1);
    }
    rem.erase(val);
  }
  cout << ans << '\n';
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long T = 1;
  cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}
