#include <bits/stdc++.h>
using ll = long long;
using namespace std;
const long double PI = acos(-1);
const long long M = 998244353;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline long long mod(long long n, long long m = M) {
  long long ret = n % m;
  if (ret < 0) ret += m;
  return ret;
}
long long exp(long long n, long long k, long long m = M) {
  if (k == 0) return 1;
  if (k == 1) return n;
  long long ax = exp(n, k / 2, m);
  ax = mod(ax * ax, m);
  if (k % 2) ax = mod(ax * n, m);
  return ax;
}
long long gcd(long long a, long long b) {
  if (a == 0)
    return b;
  else
    return gcd(b % a, a);
}
void solution() {
  list<long long> l;
  long long n, k;
  cin >> n >> k;
  vector<list<long long>::iterator> elems(n + 1);
  for (long long i = 0; i < n; i++) {
    long long x;
    cin >> x;
    l.push_back(x);
    auto ed = l.end();
    ed--;
    elems[x] = ed;
  }
  long long ans = 1;
  vector<bool> mark(n + 1, true);
  vector<long long> b(k);
  for (long long i = 0; i < k; i++) {
    cin >> b[i];
    mark[b[i]] = false;
  }
  for (long long i = 0; i < k; i++) {
    auto it = elems[b[i]];
    long long mult = 0;
    if (it != l.begin()) {
      auto it2 = it;
      it2--;
      mult += mark[*it2];
    }
    auto it2 = it;
    it2++;
    if (it2 != l.end()) mult += mark[*it2];
    ans *= mult;
    ans = mod(ans);
    mark[b[i]] = true;
  }
  cout << ans << '\n';
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  cin >> t;
  while (t--) solution();
}
