#include <bits/stdc++.h>
using namespace std;
int n, k, x, a[1000005], b[1000005], c[1000005], A[1000005];
int main() {
  int test;
  scanf("%d", &test);
  while (test--) {
    long long ans = 1;
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for (int i = 1; i <= n; i++) A[i] = 0, c[a[i]] = i;
    for (int i = 1; i <= k; i++) {
      scanf("%d", &b[i]);
      b[i] = c[b[i]];
      A[b[i]] = i;
    }
    for (int i = 1; i <= k; i++) {
      int s = 0;
      if (b[i] - 1 >= 1 && A[b[i] - 1] < i) s++;
      if (b[i] + 1 <= n && A[b[i] + 1] < i) s++;
      ans = 1ll * ans * s % 998244353;
    }
    printf("%lld\n", ans);
  }
}
