#include <bits/stdc++.h>
using namespace std;
int t, n, k;
vector<int> a, b, loc, bloc;
long long MOD = 998244353;
set<int> save;
void solve() {
  cin >> n >> k;
  save.clear();
  a.resize(n);
  b.resize(k);
  loc.resize(n);
  bloc.resize(k);
  for (int i = (0); i < (n); ++i) {
    cin >> a[i];
    a[i]--;
    loc[a[i]] = i;
  }
  for (int i = (0); i < (k); ++i) {
    cin >> b[i];
    b[i]--;
    save.insert(loc[b[i]]);
  }
  long long ans = 1;
  for (int i = (0); i < (k); ++i) {
    int x = loc[b[i]];
    if ((x == 0 || save.find(x - 1) != save.end()) &&
        (x == n - 1 || save.find(x + 1) != save.end())) {
      cout << 0 << '\n';
      return;
    } else {
      if ((x == 0 || save.find(x - 1) != save.end()) ||
          (x == n - 1 || save.find(x + 1) != save.end()))
        ;
      else
        ans = (ans * 2) % MOD;
      save.erase(x);
    }
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> t;
  while (t--) {
    solve();
  }
}
