#include <bits/stdc++.h>
using namespace std;
long long mod = 998244353;
long long fastPower(long long base, long long power) {
  long long result = 1;
  while (power > 0) {
    if (power & 1) {
      result = result * base % mod;
    }
    power >>= 1;
    base = base * base % mod;
  }
  return result;
}
long long a[200005], b[200005];
long long index1[200005];
int main() {
  long long t;
  scanf("%lld", &t);
  while (t--) {
    long long k, n;
    scanf("%lld%lld", &n, &k);
    for (long long i = 0; i < n; i++) {
      scanf("%lld", &a[i]);
    }
    for (long long i = 0; i < k; i++) {
      scanf("%lld", &b[i]);
    }
    for (long long i = 0; i <= n; i++) {
      index1[i] = 0;
    }
    for (long long i = 0; i < k; i++) {
      index1[b[i]] = i + 1;
    }
    long long power = 0;
    if (index1[a[0]] > 0 && index1[a[0]] < index1[a[1]]) power = -1;
    if (index1[a[n - 1]] > 0 && index1[a[n - 1]] < index1[a[n - 2]]) power = -1;
    if (power == -1) {
      printf("0\n");
      continue;
    }
    for (long long i = 1; i < n - 1; i++) {
      if (index1[a[i]] > 0) {
        if (index1[a[i]] > index1[a[i - 1]] && index1[a[i]] > index1[a[i + 1]])
          power++;
        else if (index1[a[i]] < index1[a[i - 1]] &&
                 index1[a[i]] < index1[a[i + 1]]) {
          power = -1;
          break;
        }
      }
    }
    if (power == -1) {
      printf("0\n");
      continue;
    }
    long long ans = fastPower(2, power);
    printf("%lld\n", ans);
  }
  return 0;
}
