#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
int T, n, a[N], b[N], K, id[N], tag[N];
template <class I>
inline I Max(I p, I q) {
  return p > q ? p : q;
}
template <class I>
inline I Min(I p, I q) {
  return p < q ? p : q;
}
inline int ksm(register int x, register int L) {
  register int ans = 1;
  while (L)
    L& 1 ? ans = (0ll + ans) * x % 998244353 : 0, x = (0ll + x) * x % 998244353,
           L >>= 1;
  return ans;
}
int main() {
  scanf("%d", &T);
  register int i;
  while (T--) {
    scanf("%d%d", &n, &K);
    register int cn = 0, x;
    for (i = 1; i <= n; ++i) scanf("%d", &a[i]), id[i] = tag[i] = 0;
    for (i = 1; i <= K; ++i) scanf("%d", &b[i]);
    for (i = 1; i <= n; ++i) id[a[i]] = i;
    for (i = 1; i <= K; ++i) tag[id[b[i]]] = 1;
    tag[0] = tag[n + 1] = 1;
    for (i = 1; i <= K; ++i) {
      x = tag[id[b[i]] - 1] + tag[id[b[i]] + 1];
      if (x == 2) break;
      cn += (!x), tag[id[b[i]]] = 0;
    }
    i <= K ? puts("0") : printf("%d\n", ksm(2, cn));
  }
  return 0;
}
