#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
int T, N, K, a[200005], id[200005], b[200005];
void solve() {
  cin >> N >> K;
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
    id[i] = 0;
  }
  for (int i = 1; i <= K; i++) {
    cin >> b[i];
    id[b[i]] = i;
  }
  int ans = 1;
  for (int i = 1; i <= N; i++) {
    if (id[a[i]]) {
      int mlt = 0;
      if (i - 1 >= 1 && id[a[i - 1]] < id[a[i]]) mlt++;
      if (i + 1 <= N && id[a[i + 1]] < id[a[i]]) mlt++;
      ans = (ans * mlt) % MOD;
    }
  }
  cout << ans << "\n";
}
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cin >> T;
  while (T--) solve();
  return 0;
}
