#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1e9;
const int mod = 998244353;
const double eps = 1e-9;
const double pi = acos(-1.0);
int n, k, a[200200], b[200200];
void solve() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", a + i);
  }
  for (int i = 0; i < k; i++) {
    scanf("%d", b + i);
  }
  set<int> second;
  map<int, int> pos, exist;
  for (int i = 0; i < n; i++) {
    second.insert(i);
    pos[a[i]] = i;
  }
  for (int i = 0; i < k; i++) {
    exist[b[i]] = 1;
  }
  int ans = 1;
  for (int i = 0; i < k; i++) {
    int x = b[i];
    __typeof(second.begin()) it = second.find(pos[x]);
    __typeof(second.begin()) it2, rem;
    int cnt = 0;
    if (it != second.begin()) {
      it2 = it;
      it2--;
      if (!exist[a[*it2]]) {
        cnt++;
        rem = it2;
      }
    }
    it2 = it;
    it2++;
    if (it2 != second.end()) {
      if (!exist[a[*it2]]) {
        cnt++;
        rem = it2;
      }
    }
    if (cnt == 0) {
      printf("0\n");
      return;
    }
    if (cnt == 2) ans = (ans + ans) % mod;
    second.erase(rem);
    exist[b[i]] = 0;
  }
  printf("%d\n", ans);
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) solve();
  return 0;
}
