#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const int maxn = 2e5 + 54;
const int maxm = 2e3 + 30;
const long long maxii = 1e18;
const int maxlog = 22;
const long long mod = (long long)998244353;
const int sq = 340;
const int inf = 1e9 + 43;
const long double PI = 3.141592653589793238463;
int place[maxn];
bool in[maxn];
bool inset[maxn];
int b[maxn];
set<pair<int, int> > st;
int get_left(int x, int tof) {
  if (!inset[x]) return tof;
  set<pair<int, int> >::iterator it = st.lower_bound(make_pair(place[x], x));
  if (it == st.begin()) return tof;
  it--;
  return (*it).second;
}
int get_right(int x, int tof) {
  if (!inset[x]) return tof;
  set<pair<int, int> >::iterator it = st.lower_bound(make_pair(place[x], x));
  it++;
  if (it == st.end()) return tof;
  return (*it).second;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.precision(15);
  int t;
  cin >> t;
  while (t--) {
    st.clear();
    int n, k;
    cin >> n >> k;
    in[n + 1] = true;
    for (int i = 1; i <= n; i++) {
      in[i] = false;
      inset[i] = true;
    }
    for (int i = 1; i <= n; i++) {
      int x;
      cin >> x;
      st.insert(make_pair(i, x));
      place[x] = i;
    }
    for (int i = 1; i <= k; i++) {
      cin >> b[i];
      in[b[i]] = true;
    }
    long long ans = 1;
    for (int i = 1; i <= k; i++) {
      int x = b[i];
      int x2 = get_right(x, n + 1);
      int y2 = get_left(x, n + 1);
      if (in[x2] && in[y2]) {
        ans = 0;
      } else if (!in[x2] && !in[y2])
        ans = (ans * (long long)2) % mod;
      else if (!in[x2]) {
        inset[x2] = false;
        st.erase(make_pair(place[x2], x2));
      } else {
        inset[y2] = false;
        st.erase(make_pair(place[y2], y2));
      }
      in[x] = false;
    }
    cout << ans << endl;
  }
  return 0;
}
