#include <bits/stdc++.h>
using namespace std;
const long long MX = 2e5 + 20;
struct DSU {
  long long parent[MX];
  long long size[MX];
  long long maxVal[MX];
  long long minVal[MX];
  void make_set(long long u) {
    parent[u] = u;
    size[u] = 1;
    maxVal[u] = u;
    minVal[u] = u;
  }
  void build(long long n) {
    for (int i = 0; i < n; i++) {
      make_set(i);
    }
  }
  long long find(long long u) {
    if (u == parent[u]) {
      return parent[u];
    } else {
      return parent[u] = find(parent[u]);
    }
  }
  void join(long long u, long long v) {
    u = find(u);
    v = find(v);
    if (u != v) {
      if (size[u] > size[v]) {
        swap(u, v);
      }
      parent[u] = v;
      size[v] += size[u];
      maxVal[v] = max(maxVal[u], maxVal[v]);
      minVal[v] = min(minVal[u], minVal[v]);
    }
  }
  long long getMax(long long u) { return maxVal[find(u)]; }
  long long getMin(long long u) { return minVal[find(u)]; }
} dsu;
const long long MOD = 998244353;
void solve() {
  long long n, k;
  cin >> n >> k;
  vector<long long> a(n);
  vector<long long> pos(n);
  vector<bool> present(n, false);
  dsu.build(n + 1);
  for (long long i = 0; i < (long long)n; i++) {
    cin >> a[i];
    a[i]--;
    pos[a[i]] = i;
  }
  vector<long long> b(k);
  for (long long i = 0; i < (long long)k; i++) {
    cin >> b[i];
    b[i]--;
    present[b[i]] = true;
  }
  long long ans = 1;
  for (long long i = 0; i < k; i++) {
    long long p = pos[b[i]];
    long long l = -1;
    long long r = -1;
    long long x = dsu.find(p);
    if (x > 0) {
      l = x - 1;
    }
    if (x < n - 1) {
      r = x + 1;
    }
    if (l == -1 && r == -1) {
      ans = 0;
    } else if (l != -1 && r != -1) {
      if (present[a[l]] && present[a[r]]) {
        ans = 0;
      } else if (!present[a[l]] && !present[a[r]]) {
        dsu.join(l, l + 1);
        ans *= 2;
        ans %= MOD;
      } else if (!present[a[l]]) {
        dsu.join(l, l + 1);
      } else {
        dsu.join(r, r + 1);
      }
    } else if (l != -1) {
      if (present[a[l]]) {
        ans = 0;
      } else {
        dsu.join(l, l + 1);
      }
    } else {
      if (present[a[r]]) {
        ans = 0;
      } else {
        dsu.join(r, r + 1);
      }
    }
    present[b[i]] = false;
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long T;
  cin >> T;
  for (long long t = 0; t < (long long)T; t++) solve();
  return 0;
}
