#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer", "inline")
const long long maxn = 1000010;
const long long mod = 998244353;
const long double eps = 1e-9;
const long long inf = ((1ll << 31ll) - 1ll);
const long long INF = 1ll * mod * mod;
const long double pi = acos(-1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long qpow(long long b, long long e) {
  if (!e) return 1;
  if (e & 1) return qpow(b, e - 1) * b % mod;
  long long pwur = qpow(b, e >> 1);
  return pwur * pwur % mod;
}
long long modinv(long long x) { return qpow(x, mod - 2); }
long long tc, n, dp[maxn], a[maxn], b[maxn], id[maxn], qid[maxn], k;
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.setf(ios::fixed);
  cout.precision(0);
  srand(time(NULL));
  cin >> tc;
  while (tc--) {
    cin >> n >> k;
    for (long long i = 0; i <= n + 1; i++) {
      a[i] = b[i] = id[i] = qid[i] = dp[i] = 0;
    }
    for (long long i = 1; i <= n; i++) {
      cin >> a[i];
      id[a[i]] = i;
    }
    for (long long i = 1; i <= k; i++) {
      cin >> b[i];
      qid[id[b[i]]] = i;
    }
    bool b = 1;
    qid[0] = qid[n + 1] = n + 1;
    for (long long i = 1; i <= n; i++) {
      if (qid[i] && qid[i + 1] && qid[i - 1])
        if (qid[i] < qid[i + 1] && qid[i] < qid[i - 1]) b = 0;
    }
    if (b == 0) {
      cout << 0 << '\n';
      continue;
    }
    qid[0] = qid[n + 1] = 0;
    long long cnt = 0;
    for (long long i = 1; i <= n + 1; i++) {
      if (qid[i] == 0) dp[i + 1] = (dp[i + 1] + dp[i]) % mod;
      if (qid[i] && !qid[i - 1]) {
        cnt++;
      }
    }
    cout << qpow(2, cnt - (qid[1] != 0) - (qid[n] != 0)) << '\n';
  }
  return 0;
}
