#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n;
  long long m;
  cin >> n >> m;
  long long a[n + 2], b[m];
  map<long long, long long> idx;
  for (long long i = 0; i < n; i++) {
    cin >> a[i + 1];
    idx[a[i + 1]] = i + 1;
  }
  a[0] = 0;
  a[n + 1] = n + 1;
  set<long long> second;
  for (long long i = 0; i < m; i++) {
    cin >> b[i];
    second.insert(b[i]);
  }
  second.insert(0);
  second.insert(n + 1);
  long long ans = 1;
  for (long long i = 0; i < m; i++) {
    long long index = idx[b[i]];
    if (second.count(a[index - 1]) && second.count(a[index + 1]))
      ans = 0;
    else if (!second.count(a[index - 1]) && !second.count(a[index + 1])) {
      ans *= 2;
      ans %= 998244353;
    }
    second.erase(b[i]);
  }
  cout << ans << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  long long t;
  cin >> t;
  while (t--) solve();
}
