#include <bits/stdc++.h>
using namespace std;
vector<string> split(const string& s, char c) {
  vector<string> v;
  stringstream second(s);
  string x;
  while (getline(second, x, c)) v.emplace_back(x);
  return move(v);
}
template <typename T, typename... Args>
inline string arrStr(T arr, int n) {
  stringstream s;
  s << "[";
  for (int i = 0; i < n - 1; i++) s << arr[i] << ",";
  s << arr[n - 1] << "]";
  return s.str();
}
inline void __evars_begin(int line) { cerr << "#" << line << ": "; }
template <typename T>
inline void __evars_out_var(vector<T> val) {
  cerr << arrStr(val, val.size());
}
template <typename T>
inline void __evars_out_var(T* val) {
  cerr << arrStr(val, 10);
}
template <typename T>
inline void __evars_out_var(T val) {
  cerr << val;
}
inline void __evars(vector<string>::iterator it) { cerr << '\n'; }
template <typename T, typename... Args>
inline void __evars(vector<string>::iterator it, T a, Args... args) {
  cerr << it->substr((*it)[0] == ' ', it->length()) << "=";
  __evars_out_var(a);
  cerr << "; ";
  __evars(++it, args...);
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int inf = 998244353;
int dx[8] = {1, -1, 0, 0, 1, 1, -1, -1};
int dy[8] = {0, 0, -1, 1, 2, -2, 2, -2};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    vector<int> a(n + 4), b(k), id(n + 1);
    for (__typeof(n + 1) i = (1) - ((1) > (n + 1));
         i != (n + 1) - ((1) > (n + 1)); i += 1 - 2 * ((1) > (n + 1))) {
      cin >> a[i];
      id[a[i]] = i;
    }
    set<int> st = {0};
    for (__typeof(k) i = (0) - ((0) > (k)); i != (k) - ((0) > (k));
         i += 1 - 2 * ((0) > (k))) {
      cin >> b[i];
      st.insert(b[i]);
    }
    long long tot = 1;
    for (__typeof(k) i = (0) - ((0) > (k)); i != (k) - ((0) > (k));
         i += 1 - 2 * ((0) > (k))) {
      int index = id[b[i]];
      int cnt = st.count(a[index - 1]) + st.count(a[index + 1]);
      tot *= (2 - cnt);
      tot %= inf;
      st.erase(b[i]);
    }
    cout << tot << '\n';
  }
}
