#include <bits/stdc++.h>
using namespace std;
bool compare(pair<long long, long long> a, pair<long long, long long> b) {
  if (a.first != b.first) return a.first > b.first;
  return a.second > b.second;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long TC;
  cin >> TC;
  while (TC--) {
    long long n, m;
    cin >> n >> m;
    vector<long long> a(n), b(m);
    set<long long> s;
    map<long long, long long> apos, bpos;
    for (long long i = 0; i < n; i++) cin >> a[i], apos[a[i]] = i;
    for (long long i = 0; i < m; i++) cin >> b[i], s.insert(b[i]);
    if (n == 1) {
      cout << 0 << "\n";
      continue;
    }
    long long mod = 998244353;
    long long ans = 1;
    for (long long i = 0; i < m; i++) {
      s.erase(b[i]);
      long long pos = apos[b[i]];
      if (pos == 0 || pos == n - 1) {
        if (pos == 0 && s.count(a[1]))
          ans = 0;
        else if (pos == n - 1 && s.count(a[n - 2]))
          ans = 0;
      } else {
        if (!s.count(a[pos + 1]) && !s.count(a[pos - 1]))
          ans *= 2;
        else if (s.count(a[pos + 1]) && s.count(a[pos - 1]))
          ans = 0;
      }
      ans %= mod;
    }
    cout << ans << "\n";
  }
  return 0;
}
