#include <bits/stdc++.h>
using namespace std;
ostream& operator<<(ostream& out, const vector<long long>& b) {
  for (auto k : b) out << k << " ";
  return out;
}
istream& operator>>(istream& in, pair<long long, long long>& b) {
  in >> b.first >> b.second;
  return in;
}
ostream& operator<<(ostream& out, const pair<long long, long long>& b) {
  out << "{" << b.first << ", " << b.second << "}";
  return out;
}
template <typename T1, typename T2>
inline bool chkmin(T1& x, const T2& y) {
  if (x > y) {
    x = y;
    return 1;
  }
  return 0;
}
template <typename T1, typename T2>
inline bool chkmax(T1& x, const T2& y) {
  if (x < y) {
    x = y;
    return 1;
  }
  return 0;
}
const long long INF = 1e18;
const long double PI = 4 * atan(1);
mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count());
void solve() {
  long long n, k;
  cin >> n >> k;
  vector<long long> a(n), b(k);
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  for (long long i = 0; i < k; i++) {
    cin >> b[i];
    b[i]--;
  }
  vector<long long> ind(n, 0);
  for (long long i = 0; i < n; i++) {
    ind[a[i]] = i;
  }
  vector<long long> ban(n, 0);
  for (long long i = 0; i < k; i++) {
    ban[ind[b[i]]] = 1;
  }
  long long ans = 1;
  const long long mod = 998244353;
  for (long long i = 0; i < k; i++) {
    long long cnt = 0;
    if (ind[b[i]] > 0 && !ban[ind[b[i]] - 1]) {
      cnt++;
    }
    if (ind[b[i]] < n - 1 && !ban[ind[b[i]] + 1]) {
      cnt++;
    }
    ban[ind[b[i]]] = 0;
    ans = ans * cnt % mod;
  }
  cout << ans << "\n";
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long q = 1;
  cin >> q;
  while (q--) {
    solve();
  }
}
