#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T &ret) {
  T x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  ret = x * f;
}
template <class T>
T Pow(T a, T b, T p) {
  T ret = 1;
  a %= p;
  for (; b; b >>= 1, a = a * a % p)
    if (b & 1) ret = ret * a % p;
  return ret;
}
const long long Z = 998244353;
const int N = 200010;
int a[N], b[N], loca[N];
bool flg[N];
int n, m;
int nxt[N], prv[N];
long long solv() {
  fill(flg, flg + n + 1, 0);
  for (long long i = (1); i <= (n); i++) loca[a[i]] = i;
  for (long long i = (1); i <= (m); i++) flg[b[i] = loca[b[i]]] = 1;
  for (long long i = (1); i <= (n); i++)
    prv[i] = i - 1, nxt[i] = (i + 1) % (n + 1);
  flg[0] = 1;
  long long cnt = 0;
  for (long long i = (1); i <= (m); i++) {
    int l = prv[b[i]];
    int r = nxt[b[i]];
    if (flg[l] && flg[r]) return 0;
    if (!flg[l] && !flg[r]) cnt++;
    if (!flg[l]) {
      if (nxt[l]) prv[nxt[l]] = prv[l];
      if (prv[l]) nxt[prv[l]] = nxt[l];
    } else {
      if (nxt[r]) prv[nxt[r]] = prv[r];
      if (prv[r]) nxt[prv[r]] = nxt[r];
    }
    flg[b[i]] = 0;
  }
  return Pow((long long)2, cnt, Z);
}
int main() {
  int T;
  read(T);
  while (T--) {
    read(n);
    read(m);
    for (long long i = (1); i <= (n); i++) read(a[i]);
    for (long long i = (1); i <= (m); i++) read(b[i]);
    printf("%lld\n", solv());
  }
}
