#include <bits/stdc++.h>
using namespace std;
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
  for (auto &v : vec) is >> v;
  return is;
}
template <typename T>
void dout(string name, T arg) {
  cerr << arg << endl;
}
template <typename T1, typename... T2>
void dout(string names, T1 arg, T2... args) {
  cerr << arg << " | ";
  dout(names.substr(names.find(',') + 2), args...);
}
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
const int mx = 1e6 + 5;
const int INF = 998244353;
long long power(long long x, long long y) {
  long long res = 1;
  while (y > 0) {
    if (y & 1) res = (res * x) % 998244353;
    y >>= 1;
    x = (x * x) % 998244353;
  }
  return (res % 998244353);
}
void solve() {
  long long n, m;
  cin >> n >> m;
  vector<long long> arr(n);
  map<long long, long long> mp;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    mp[arr[i]] = i;
  }
  unordered_set<long long> st;
  vector<long long> brr(m);
  for (int i = 0; i < m; i++) {
    cin >> brr[i];
    st.insert(brr[i]);
  }
  long long ans = 1;
  for (int i = 0; i < m; i++) {
    long long pos = mp[brr[i]];
    long long ref = 0;
    if (pos > 0) {
      if (st.find(arr[pos - 1]) == st.end()) {
        ref++;
      }
    }
    if (pos < n - 1) {
      if (st.find(arr[pos + 1]) == st.end()) {
        ref++;
      }
    }
    st.erase(brr[i]);
    ans = (ans * ref) % 998244353;
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
}
