#include <bits/stdc++.h>
inline long long add(long long a, long long b) {
  a += b;
  if (a >= 1000000007) a -= 1000000007;
  return a;
}
inline long long sub(long long a, long long b) {
  a -= b;
  if (a < 0) a += 1000000007;
  return a;
}
inline long long mul(long long a, long long b) {
  return (long long)((long long)a * b % 1000000007);
}
using namespace std;
vector<long long> sieve;
void Sieve(int N) {
  const long long maxn = N;
  sieve.resize(maxn);
  for (long long(i) = 0; (i) < (maxn); ++(i)) sieve[i] = i;
  sieve[1] = -1;
  sieve[0] = -1;
  for (long long(i) = 2; (i) <= (maxn); (i) += (1))
    if (i == sieve[i])
      for (long long j = 2 * i; j < maxn; j += i)
        if (sieve[j] == j) sieve[j] = i;
}
long long extended_GCD(long long a, long long b, long long &x, long long &y) {
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  long long x1, y1;
  long long gcd = extended_GCD(b % a, a, x1, y1);
  x = y1 - (b / a) * x1;
  y = x1;
  return gcd;
}
long long power(long long a, long long b, long long m = 1000000007) {
  a %= m;
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
long long modinv(long long a, long long mod = 1000000007) {
  long long x, y;
  extended_GCD(a, mod, x, y);
  if (x < 0) x += mod;
  return x;
}
void solve() {
  long long n;
  cin >> n;
  long long k;
  cin >> k;
  long long a[n];
  long long b[k];
  map<long long, long long> ma, ma1;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    ma[a[i]] = i;
  }
  map<long long, long long> mm;
  for (int i = 0; i < k; ++i) {
    cin >> b[i];
    b[i] = ma[b[i]];
    ma1[b[i]] = i + 1;
    mm[b[i]] = 1;
  }
  for (int i = 0; i < k; ++i) {
    if (mm[b[i] - 1] && mm[b[i] + 1]) {
      cout << 0 << "\n";
      return;
    }
    mm[b[i]] = 0;
  }
  if (n == 2) {
    cout << 1 << "\n";
    return;
  }
  for (int i = 0; i < n; ++i) {
    if (ma1[i] == 0) break;
    if (ma1[i] < ma1[i + 1]) {
      cout << 0 << "\n";
      return;
    }
  }
  for (int i = n - 1; i >= 0; --i) {
    if (ma1[i] == 0) break;
    if (ma1[i] < ma1[i - 1]) {
      cout << 0 << "\n";
      return;
    }
  }
  sort(b, b + k);
  long long ans = 1;
  long long x = 1;
  for (int i = 1; i < k; ++i) {
    if (b[i] != b[i - 1] + 1) x++;
  }
  if (ma1[0]) x--;
  if (ma1[n - 1]) x--;
  for (int i = 0; i < x; ++i) {
    ans *= 2;
    ans %= 998244353;
  }
  cout << ans << "\n";
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
