#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  for (; b; a %= b, swap(a, b))
    ;
  return a;
}
const long long MOD = 998244353;
int n, k;
int a[200001];
int idx[200001];
int b[200001];
bool cant[200001];
set<pair<int, int> > st;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    st.clear();
    cin >> n >> k;
    for (int i = 1; i <= n; i++) cant[i] = 0;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      idx[a[i]] = i;
      st.insert({i, a[i]});
    }
    long long ans = 1;
    for (int i = 0; i < k; i++) {
      cin >> b[i];
      cant[b[i]] = 1;
    }
    for (int i = 0; i < k; i++) {
      int cn = b[i];
      int ci = idx[b[i]];
      auto it = st.find({ci, cn});
      bool left = true, right = true;
      if (it == st.begin())
        left = false;
      else if (cant[prev(it)->second])
        left = false;
      if (next(it) == st.end())
        right = false;
      else if (cant[next(it)->second])
        right = false;
      if (left && right) {
        ans = ans * 2 % MOD;
        st.erase(prev(it));
      } else if (left) {
        st.erase(prev(it));
      } else if (right) {
        st.erase(next(it));
      } else {
        ans = 0;
        break;
      }
      cant[cn] = 0;
    }
    cout << ans << '\n';
  }
}
