#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
void solve() {
  long long n, k;
  cin >> n >> k;
  vector<long long> a(n + 1), pos(n + 1), occ(n + 1, -1), b(k + 1);
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    pos[a[i]] = i;
  }
  for (long long i = 1; i <= k; i++) {
    cin >> b[i];
    occ[b[i]] = i;
  }
  long long ans = 1;
  for (long long i = 1; i <= k; i++) {
    long long c = 0;
    if (pos[b[i]] > 1 && occ[a[pos[b[i]] - 1]] < i) ++c;
    if (pos[b[i]] < n && occ[a[pos[b[i]] + 1]] < i) ++c;
    ans = (ans * c) % mod;
  }
  cout << ans << "\n";
}
int32_t main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long test, tc = 0;
  cin >> test;
  while (test--) {
    solve();
  }
}
