#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const long double pi = acos((ld)-1.0);
const double eps = (double)1e-9;
const int inf = 1e9 + 7;
const int mod = 998244353;
int main() {
  int t;
  cin >> t;
  while (t-- > 0) {
    int n, m;
    cin >> n >> m;
    vector<int> a(n), pos(n + 1);
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      pos[a[i]] = i;
    }
    vector<int> b(m);
    set<int> st;
    for (int i = 0; i < m; i++) {
      cin >> b[i];
      st.insert(b[i]);
    }
    int ans = 1;
    for (int i = 0; i < m; i++) {
      st.erase(b[i]);
      int cnt = 0, j = pos[b[i]];
      if (j > 0 and st.find(a[j - 1]) == st.end()) {
        cnt++;
      }
      if (j < n - 1 and st.find(a[j + 1]) == st.end()) {
        cnt++;
      }
      ans = (ans * cnt) % mod;
    }
    cout << ans % mod << endl;
  }
  return 0;
}
const int fastio = []() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  return 0;
}();
