#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int T;
int n, k;
int a[200010];
int b[200010];
map<int, int> q;
set<int> in;
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d%d", &n, &k);
    q.clear();
    in.clear();
    for (int i = 1; i <= n; i++) scanf("%d", a + i), q[a[i]] = i;
    for (int i = 1; i <= k; i++) {
      scanf("%d", b + i);
      in.insert(b[i]);
    }
    a[0] = a[n + 1] = 10;
    for (int i = 1; i <= n; i++) {
      if (!in.count(a[i])) a[i] = 0;
    }
    int ans = 1;
    for (int i = 1; i <= k; i++) {
      int pos = q[b[i]];
      int p = 0;
      if (a[pos - 1] == 0) p++;
      if (a[pos + 1] == 0) p++;
      if (p == 0)
        ans = 0;
      else if (p == 2)
        ans = ans * 2 % mod;
      a[pos] = 0;
    }
    printf("%d\n", ans);
  }
  return 0;
}
