#include <bits/stdc++.h>
using namespace std;
long long a[300000];
long long b[300000];
long long ind[300000];
void solve() {
  long long n, k;
  cin >> n >> k;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
  };
  for (long long i = 0; i < k; i++) {
    cin >> b[i];
  };
  for (long long i = 0; i < n + 10; i++) ind[i] = -1;
  for (long long i = 0; i < k; i++) ind[b[i]] = i;
  long long ans = 1;
  long long p = 998244353;
  for (long long i = 0; i < n; i++) {
    if (ind[a[i]] == -1) continue;
    long long prev = ((i == 0) ? n + 2 : ind[a[i - 1]]);
    long long next = ((i == n - 1) ? n + 2 : ind[a[i + 1]]);
    long long val = 0;
    if (prev < ind[a[i]]) val++;
    if (next < ind[a[i]]) val++;
    ans *= val;
    ans = ans % p;
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  long long tc;
  cin >> tc;
  while (tc--) {
    solve();
  }
}
