#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0;
  bool f = false;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = true;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + (c ^ '0');
    c = getchar();
  }
  return f ? -x : x;
}
inline void write(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
const long long mod = 998244353, N = 2e6 + 10;
long long n, k, a[N], b[N], ans = 1, l[N], r[N], t;
bool f[N];
inline void del(long long x) {
  r[l[x]] = r[x];
  l[r[x]] = l[x];
  l[x] = 0;
  r[x] = n + 1;
}
signed main() {
  t = read();
  while (t--) {
    n = read();
    k = read();
    ans = 1;
    for (register long long i = 1; i <= n; ++i)
      a[read()] = i, l[i] = i - 1, r[i] = i + 1;
    for (register long long i = 1; i <= k; ++i) b[i] = a[read()], f[b[i]] = 1;
    for (register long long i = 1; i <= k; ++i) {
      register long long cnt = 0, L = l[b[i]], R = r[b[i]];
      f[b[i]] = 0;
      if (L >= 1 && !f[L]) ++cnt;
      if (R <= n && !f[R]) ++cnt;
      ans = ans * cnt % mod;
      if (L >= 1 && !f[L])
        del(L);
      else if (R <= n && !f[R])
        del(R);
    }
    cout << ans << '\n';
  }
  return 0;
}
