#include <bits/stdc++.h>
using namespace std;
const long long N = 200010, Mod = 998244353;
inline long long read() {
  long long s = 0, w = 1;
  register char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar();
  return s * w;
}
long long n, K, a[N], b[N], pos[N];
set<long long> Q;
signed main() {
  for (register long long T = read(); T; T--) {
    n = read(), K = read();
    for (register long long i = 1; i <= n; i++) pos[i] = 0;
    for (register long long i = 1; i <= n; i++) a[i] = read(), pos[a[i]] = i;
    for (register long long i = 1; i <= K; i++) b[i] = read();
    long long res = 1;
    Q.clear();
    for (register long long i = 1; i <= K; i++) Q.insert(b[i]);
    for (register long long i = 1; i <= K; i++) {
      long long x = b[i];
      Q.erase(Q.find(x));
      long long j = pos[x];
      long long now = 2;
      if (j < n) {
        if (Q.find(a[j + 1]) != Q.end()) now--;
      } else
        now--;
      if (j > 1) {
        if (Q.find(a[j - 1]) != Q.end()) now--;
      } else
        now--;
      res = res * now % Mod;
    }
    printf("%lld\n", res);
  }
  return 0;
}
