#include <bits/stdc++.h>
using namespace std;
void c_p_c() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int32_t main() {
  c_p_c();
  long long int t;
  cin >> t;
  while (t--) {
    long long int n, k;
    cin >> n >> k;
    vector<long long int> a(n), b(k);
    for (long long int i = 0; i < (long long int)n; i++) cin >> a[i];
    for (long long int i = 0; i < (long long int)k; i++) cin >> b[i];
    map<long long int, long long int> pos, present;
    for (auto it : b) present[it] = 1;
    for (long long int i = 0; i < (long long int)n; i++) pos[a[i]] = i;
    long long int ans = 1;
    for (long long int i = 0; i < (long long int)k; i++) {
      long long int p = pos[b[i]];
      long long int temp = 0;
      if (p == 0) {
        if (!present[a[1]]) temp++;
      } else if (p == n - 1) {
        if (!present[a[n - 2]]) temp++;
      } else {
        if (!present[a[p - 1]]) temp++;
        if (!present[a[p + 1]]) temp++;
      }
      ans = (ans * temp) % 998244353;
      present[b[i]] = 0;
    }
    cout << ans << "\n";
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\n";
  return 0;
}
