#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long inf = 5e18;
const long long mod1 = 998244353;
const long long nax = 200005;
long long n, k, a[nax], b[nax], pos[nax];
set<long long> s;
void solve() {
  cin >> n >> k;
  for (long long i = (0); i < (n); i++) {
    cin >> a[i];
    pos[a[i]] = i;
  }
  for (long long i = (0); i < (k); i++) {
    cin >> b[i];
    s.insert(b[i]);
  }
  long long ans = 1;
  for (long long i = (0); i < (k); i++) {
    long long now = pos[b[i]];
    long long cnt = 0;
    if (now && s.count(a[now - 1]) == 0) cnt++;
    if (now != n - 1 && s.count(a[now + 1]) == 0) cnt++;
    ans = (ans * cnt) % mod1;
    s.erase(b[i]);
  }
  cout << ans << '\n';
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) solve();
  return 0;
}
