#include <bits/stdc++.h>
const int MAXN = 2e5 + 19, MOD = 998244353;
int n, k;
int a[MAXN], b[MAXN];
bool vist[MAXN];
int next[MAXN], prev[MAXN];
int getn(int node) {
  return next[node] == node ? node : next[node] = getn(next[node]);
}
int getp(int node) {
  return prev[node] == node ? node : prev[node] = getp(prev[node]);
}
int pos[MAXN];
int main() {
  int T;
  std::scanf("%d", &T);
  while (T--) {
    std::scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; ++i)
      std::scanf("%d", a + i), pos[a[i]] = i, vist[i] = 0;
    for (int i = 1; i <= k; ++i)
      std::scanf("%d", b + i), b[i] = pos[b[i]], vist[b[i]] = 1;
    vist[0] = vist[n + 1] = 1;
    for (int i = 0; i <= n + 1; ++i) prev[i] = i, next[i] = i;
    int ans = 1;
    for (int i = 1; i <= k; ++i) {
      int c = int(!vist[getp(b[i] - 1)]) + int(!vist[getn(b[i] + 1)]);
      ans = (long long)ans * c % MOD;
      prev[b[i]] = getp(b[i] - 1), next[b[i]] = getn(b[i] + 1);
    }
    std::printf("%d\n", ans);
  }
  return 0;
}
