#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
const int mod = 998244353;
int a[maxn], b[maxn], pos[maxn], ok[maxn];
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      pos[a[i]] = i;
      ok[i] = 1;
    }
    for (int i = 1; i <= k; i++) {
      cin >> b[i];
      ok[pos[b[i]]] = 0;
    }
    long long ans = 1;
    for (int i = 1; i <= k; i++) {
      int f = 0;
      if (pos[b[i]] > 1 && ok[pos[b[i]] - 1]) f++;
      if (pos[b[i]] < n && ok[pos[b[i]] + 1]) f++;
      ans *= f;
      ans %= mod;
      ok[pos[b[i]]] = 1;
    }
    cout << ans << endl;
  }
  return 0;
}
