#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T& x) {
  T f = 1;
  x = 0;
  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();
  }
  x = x * f;
}
template <class T>
inline void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
const int maxn = 2e5 + 5;
int T, a[maxn], b[maxn], n, k, c[maxn], d[maxn];
const int mod = 998244353;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> T;
  while (T--) {
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      d[a[i]] = i;
    }
    for (int i = 1; i <= k; i++) {
      cin >> b[i];
      c[b[i]] = 1;
    }
    int ans = 1ll;
    for (int i = 1; i <= k; i++) {
      int ind = d[b[i]];
      int tmp = 0;
      if (ind > 1 && !c[a[ind - 1]]) tmp++;
      if (ind < n && !c[a[ind + 1]]) tmp++;
      ans *= tmp;
      ans %= mod;
      c[b[i]] = 0;
    }
    cout << ans << "\n";
  }
  return 0;
}
