#include <bits/stdc++.h>
const long long INF = LLONG_MAX / 2;
const long long N = 2e5 + 1;
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t = 1;
  std::cin >> t;
  while (t--) {
    long long n, i, j = 0, k;
    std::cin >> n >> k;
    long long c[n + 1];
    std::vector<long long> a;
    map<long long, long long> mp, mp1;
    for (long long i = 0; i < n; i++) {
      cin >> j;
      a.push_back(j);
      mp[a[i]]++;
      c[a[i]] = i;
    }
    long long b[k], ans = 1;
    for (long long i = 0; i < k; i++) {
      cin >> b[i];
      mp1[b[i]]++;
    }
    for (long long i = 0; i < k; i++) {
      long long index = c[b[i]];
      if ((index + 1 < n && mp1.find(a[index + 1]) == mp1.end()) &&
          (index - 1 >= 0 && mp1.find(a[index - 1]) == mp1.end())) {
        mp1.erase(a[index]);
        ans = (ans * 2) % 998244353;
        continue;
      } else if ((index + 1 < n && mp1.find(a[index + 1]) == mp1.end())) {
        mp1.erase(a[index]);
        continue;
      } else if ((index - 1 >= 0 && mp1.find(a[index - 1]) == mp1.end())) {
        mp1.erase(a[index]);
      } else {
        ans = 0;
        break;
      }
    }
    cout << ans << "\n";
  }
}
