#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
  x = 0;
  char ch = getchar();
  long long f = 1;
  while (!isdigit(ch)) {
    if (ch == '-') f *= -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  x *= f;
}
int a[200010];
int b[200010];
int lf[200010], rg[200010];
int pre[200010], nxt[200010];
bool vis[200010];
int main() {
  int t;
  int n, m;
  const long long p = 998244353;
  scanf("%d", &t);
  while (t--) {
    scanf("%d%d", &n, &m);
    memset(vis, 0, sizeof(bool) * (n + 1));
    memset(lf, 0, sizeof(int) * (n + 1));
    memset(rg, 0, sizeof(int) * (n + 1));
    memset(pre, 0, sizeof(int) * (n + 1));
    memset(nxt, 0, sizeof(int) * (n + 1));
    for (int i = 1; i <= n; ++i) {
      scanf("%d", &a[i]);
    }
    for (int i = 1; i <= m; ++i) {
      scanf("%d", &b[i]);
      vis[b[i]] = true;
    }
    int sum = 0, pe = 0;
    for (int i = 1; i <= n; ++i) {
      int x = a[i];
      if (!vis[x]) {
        ++sum;
        continue;
      }
      lf[x] = sum;
      rg[pe] = sum;
      pre[x] = pe;
      nxt[pe] = x;
      sum = 0;
      pe = x;
    }
    rg[pe] = sum;
    long long ans = 1;
    for (int i = 1; i <= m; ++i) {
      int x = b[i];
      if (lf[x] == 0 && rg[x] == 0) {
        ans = 0;
        break;
      }
      if (lf[x] && rg[x]) {
        ans = ans * 2 % p;
      }
      int pe = pre[x], nt = nxt[x];
      rg[pe] += rg[x];
      lf[nt] += lf[x];
      nxt[pe] = nt;
      pre[nt] = pe;
    }
    printf("%lld\n", ans);
  }
  return 0;
}
