#include <bits/stdc++.h>
using namespace std;
long long bigmod(long long b, long long p, long long md) {
  if (p == 0) return 1;
  if (p % 2 == 1) {
    return ((b % md) * bigmod(b, p - 1, md)) % md;
  } else {
    long long y = bigmod(b, p / 2, md);
    return (y * y) % md;
  }
}
long long pos[1000002];
bool ok[1000002];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  ;
  long long t;
  cin >> t;
  while (t--) {
    long long n, k;
    cin >> n >> k;
    long long arr[n];
    long long brr[k];
    for (long long i = 1; i <= n; i++) {
      pos[i] = -1;
      ok[i] = false;
    }
    for (long long i = 0; i < n; i++) {
      cin >> arr[i];
      pos[arr[i]] = i;
    }
    for (long long i = 0; i < k; i++) {
      cin >> brr[i];
      ok[brr[i]] = true;
    }
    long long ans = 1;
    for (long long i = 0; i < k; i++) {
      long long val = brr[i];
      long long id = pos[val];
      if (id == -1) {
        ans = 0;
        continue;
      }
      long long here = 0;
      if (id - 1 >= 0 && ok[arr[id - 1]] == false) {
        here++;
      }
      if (id + 1 < n && ok[arr[id + 1]] == false) {
        here++;
      }
      ans = ans * here;
      ans = ans % 998244353;
      ok[val] = false;
    }
    cout << ans << endl;
  }
  return 0;
}
