#include <bits/stdc++.h>
using namespace std;
int t;
int a[220000], b[220000], d[220000];
bool c[220000];
int main() {
  cin >> t;
  for (int i = 1; i <= t; i++) {
    int n, k;
    cin >> n >> k;
    memset(c, false, sizeof c);
    c[0] = true;
    c[n + 1] = true;
    long long ans = 1;
    for (int j = 1; j <= n; j++) {
      cin >> a[j];
      b[a[j]] = j;
    }
    for (int j = 1; j <= k; j++) {
      cin >> d[j];
    }
    for (int j = k; j >= 1; j--) {
      int possible = 2;
      c[b[d[j]]] = true;
      if (c[b[d[j]] - 1]) possible--;
      if (c[b[d[j]] + 1]) possible--;
      ans *= possible;
      if (ans == 0) break;
      ans = ans % 998244353;
    }
    cout << ans << endl;
  }
  return 0;
}
