#include <bits/stdc++.h>
using namespace std;
const int64_t INF = (int64_t)(2e18);
const int inf = (int)(1e9 + 7);
const int maxn = 500 * 1000 + 100;
chrono::time_point<chrono::steady_clock> cl;
double current_time() {
  return (chrono::steady_clock::now() - cl).count() / 1e9;
}
const int mod = 998244353;
void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> ind(n);
  set<int> usable;
  usable.insert(-inf);
  usable.insert(inf);
  set<pair<int, int>> s;
  s.insert(make_pair(-inf, -inf));
  s.insert(make_pair(inf, inf));
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    --a;
    ind[a] = i;
    s.insert(make_pair(ind[a], a));
  }
  vector<int> b(k);
  for (int i = 0; i < k; ++i) {
    cin >> b[i];
    --b[i];
    usable.insert(b[i]);
  }
  int res = 1;
  for (auto& it : b) {
    auto iter = s.find(make_pair(ind[it], it));
    auto iterl = iter;
    auto iterr = iter;
    --iterl;
    ++iterr;
    bool ul = false;
    bool ur = false;
    if (usable.count(iterl->second)) ul = true;
    if (usable.count(iterr->second)) ur = true;
    if (ul && ur) return void(cout << (0) << endl);
    int cnt = (1 - ul) + (1 - ur);
    res = res * cnt % mod;
    usable.erase(it);
    if (!ul)
      s.erase(iterl);
    else
      s.erase(iterr);
  }
  cout << res << endl;
}
int32_t main() {
  cl = chrono::steady_clock::now();
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  cin.tie(nullptr);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
