#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
static inline void amin(T &x, U y) {
  if (y < x) x = y;
}
template <typename T, typename U>
static inline void amax(T &x, U y) {
  if (y > x) x = y;
}
const long long inf = (long long)1e18 + 17;
const long long iinf = 1500000000;
const long long dx[] = {1, 0, -1, 0};
const long long dy[] = {0, 1, 0, -1};
const long long mod = 998244353;
bool test_case = 1;
void solve() {
  long long n, k;
  cin >> n >> k;
  long long a[n + 10], b[k + 10], inda[n + 10];
  for (long long i = 1; i <= n; i++) cin >> a[i], inda[a[i]] = i;
  ;
  for (long long i = 0; i < k; i++) cin >> b[i];
  long long peek[n + 10];
  for (long long i = 0; i <= n + 1; i++) peek[i] = 1;
  peek[0] = peek[n + 1] = 0;
  for (long long i = 0; i < k; i++) {
    peek[inda[b[i]]] = 0;
  }
  set<pair<long long, long long> > s;
  for (long long i = 0; i <= n + 1; i++) {
    s.insert({i, peek[i]});
  }
  long long ans = 1;
  for (long long i = 0; i < k; i++) {
    long long pos = inda[b[i]];
    long long val = peek[pos - 1] + peek[pos + 1];
    if (val == 0) {
      cout << "0\n";
      return;
    }
    peek[pos] = 1;
    ans = (ans * val) % mod;
  }
  cout << ans << endl;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  if (test_case == true) cin >> t;
  for (long long i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}
