#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10, mod = 998244353;
int T, n, k, a[maxn], b[maxn], c[maxn], p[maxn];
int Find(int x) { return *lower_bound(b + 1, b + k + 1, x) == x; }
long long Qpow(long long base, long long x) {
  long long ret = 1;
  while (x) {
    if (x & 1) ret = ret * base % mod;
    base = base * base % mod;
    x >>= 1;
  }
  return ret;
}
int main() {
  for (scanf("%d", &T); T; T--) {
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; i++) scanf("%d", a + i);
    for (int i = 1; i <= k; i++) scanf("%d", b + i), p[b[i]] = i;
    sort(b + 1, b + 1 + k);
    b[k + 1] = 0;
    for (int i = 1; i <= n; i++) c[i] = Find(a[i]);
    int ans = 0, sta = 0;
    for (int i = 2; i < n; i++) {
      if (c[i - 1] && c[i] && c[i + 1] && p[a[i - 1]] > p[a[i]] &&
          p[a[i + 1]] > p[a[i]])
        sta = 1;
    }
    if (c[2] && c[1] && p[a[2]] > p[a[1]]) sta = 1;
    if (c[n] && c[n - 1] && p[a[n - 1]] > p[a[n]]) sta = 1;
    if (sta) {
      puts("0");
      continue;
    }
    for (int i = 2; i <= n; i++)
      if (!c[i - 1] && c[i]) ans++;
    if (c[n]) ans--;
    printf("%lld\n", Qpow(2, ans));
  }
  return 0;
}
