#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << p.first << " " << p.second;
  return os;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++) {
    os << v[i] << (i + 1 != (int)v.size() ? " " : "");
  }
  return os;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (T &in : v) is >> in;
  return is;
}
const int mod = 998244353;
int tag[200200], pos[200200];
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    vector<int> a(n), b(k);
    cin >> a >> b;
    for (int i = 0; i < n; ++i) {
      pos[a[i]] = i + 1;
    }
    for (int i = 0; i < k; ++i) {
      b[i] = pos[b[i]];
    }
    memset(tag, 0, (n + 2) << 2);
    tag[0] = tag[n + 1] = 1;
    for (int i = 0; i < k; ++i) {
      tag[b[i]] = 1;
    }
    int ans = 1;
    for (int i = 0; i < k; ++i) {
      if (tag[b[i] - 1] && tag[b[i] + 1]) {
        ans = 0;
        break;
      }
      if (!tag[b[i] - 1] && !tag[b[i] + 1]) {
        ans <<= 1;
        ans %= mod;
      }
      tag[b[i]] = 0;
    }
    cout << ans << endl;
  }
}
