#include <bits/stdc++.h>
using namespace std;
const long long N = 2e6 + 7, M = 998244353, inf = INT_MAX;
const long long INF = LLONG_MAX;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  if (1) return;
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  if (1) return;
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
void print(long long f) {
  if (f)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void solve() {
  long long n, m;
  cin >> n >> m;
  long long arr[n], brr[m];
  map<long long, long long> idx, r;
  for (long long i = 0; i < n; i++) cin >> arr[i], idx[arr[i]] = i;
  for (long long i = 0; i < m; i++) cin >> brr[i], r[brr[i]] = i;
  long long ans = 1;
  for (long long i = 0; i < (long long)m; i++) {
    long long choice = 2;
    long long y = idx[brr[i]];
    if (y == 0 or r[arr[y - 1]] > r[brr[i]]) choice--;
    if (y == n - 1 or r[arr[y + 1]] > r[brr[i]]) choice--;
    __f("brr[i], choice", brr[i], choice);
    ans *= choice;
    ans %= M;
  }
  cout << ans << endl;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long test = 1;
  cin >> test;
  while (test--) solve();
}
