#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int T, N, K, A[200005], B[200005], idx[200005];
bool chk[200005];
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> T;
  while (T--) {
    cin >> N >> K;
    for (int i = 1; i <= N; i++) cin >> A[i], chk[i] = 0, idx[A[i]] = i;
    for (int i = 1; i <= K; i++) cin >> B[i], chk[B[i]] = 1;
    ll ans = 1LL;
    for (int i = 1; i <= K; i++) {
      int j = idx[B[i]];
      int cur = 0;
      if (j > 1 && !chk[A[j - 1]]) cur++;
      if (j < N && !chk[A[j + 1]]) cur++;
      ans = ans * cur % 998244353;
      chk[B[i]] = 0;
    }
    cout << ans << "\n";
  }
}
