#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, k;
  cin >> n >> k;
  int a[n];
  int adj[200001][2];
  set<int> as;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    as.insert(a[i]);
  }
  for (int i = 0; i < n; i++) {
    if (i != n - 1 && i != 0) adj[a[i]][0] = a[i - 1], adj[a[i]][1] = a[i + 1];
  }
  adj[a[0]][0] = -1;
  adj[a[n - 1]][1] = -1;
  adj[a[0]][1] = a[1];
  adj[a[n - 1]][0] = a[n - 2];
  set<int> bs;
  int b[k];
  for (int i = 0; i < k; ++i) {
    cin >> b[i];
    bs.insert(b[i]);
    if (as.find(b[i]) == as.end()) {
      cout << 0 << endl;
      return;
    }
  }
  long long int ans = 1;
  for (int i = 0; i < k; i++) {
    if (adj[b[i]][1] == -1) {
      if (bs.find(adj[b[i]][0]) != bs.end()) {
        ans = 0;
        break;
      }
      bs.erase(b[i]);
      adj[b[i]][0] = adj[adj[b[i]][0]][0];
      adj[adj[b[i]][0]][1] = b[i];
    } else if (adj[b[i]][0] == -1) {
      if (bs.find(adj[b[i]][1]) != bs.end()) {
        ans = 0;
        break;
      }
      bs.erase(b[i]);
      adj[b[i]][1] = adj[adj[b[i]][1]][1];
      adj[adj[b[i]][1]][0] = b[i];
    } else {
      if ((bs.find(adj[b[i]][0]) != bs.end()) &&
          (bs.find(adj[b[i]][1]) != bs.end())) {
        ans = 0;
        break;
      } else if ((bs.find(adj[b[i]][0]) != bs.end())) {
        bs.erase(b[i]);
        adj[b[i]][1] = adj[adj[b[i]][1]][1];
        adj[adj[b[i]][1]][0] = b[i];
      } else if (bs.find(adj[b[i]][1]) != bs.end()) {
        bs.erase(b[i]);
        adj[b[i]][0] = adj[adj[b[i]][0]][0];
        adj[adj[b[i]][0]][1] = b[i];
      } else {
        ans *= 2LL;
        ans %= 998244353;
        bs.erase(b[i]);
        adj[b[i]][0] = adj[adj[b[i]][0]][0];
        adj[adj[b[i]][0]][1] = b[i];
      }
    }
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
