#include <bits/stdc++.h>
using namespace std;
int k, a[200001], b[200001], d[200001];
int solve() {
  int n;
  cin >> n >> k;
  vector<int> c(n + 1), cnt(n + 1), chk(n + 1);
  long long ans = 1;
  for (int i = 0; i < n; i++) cin >> a[i], d[a[i]] = i;
  for (int i = 0; i < k; i++) cin >> b[i];
  for (int i = k - 1; i >= 0; i--) {
    chk[b[i]] = -1;
    int idx = d[b[i]], ok = 1;
    if (idx > 0 && chk[a[idx - 1]] != -1) {
      cnt[b[i]] += ok;
    }
    if (idx < n - 1 && chk[a[idx + 1]] != -1) {
      cnt[b[i]] += ok;
    }
    ans = (ans * cnt[b[i]]) % 998244353;
    if (ans == 0) break;
  }
  return ans;
}
int main(void) {
  int t;
  cin >> t;
  while (t--) {
    int ans = solve();
    cout << ans << '\n';
  }
  return 0;
}
