#include <bits/stdc++.h>
using namespace std;
struct index {
  long long prev;
  long long next;
};
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long T;
  cin >> T;
  while (T--) {
    long long n, k, i;
    cin >> n >> k;
    struct index ind[n + 2];
    long long pos[n + 1];
    bool deletable[n + 2];
    deletable[0] = deletable[n + 1] = 0;
    for (i = 1; i <= n; i++) {
      long long t;
      cin >> t;
      pos[t] = i;
      deletable[i] = 1;
    }
    long long b[k];
    for (i = 0; i < k; i++) {
      cin >> b[i];
      deletable[pos[b[i]]] = 0;
    }
    long long ans = 1;
    for (i = 0; i < k; i++) {
      long long y = pos[b[i]] + 1;
      long long x = pos[b[i]] - 1;
      ans *= ((deletable[x]) + (deletable[y]));
      ans %= 998244353;
      deletable[pos[b[i]]] = 1;
    }
    cout << ans << endl;
  }
  return 0;
}
