#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const long long N = 300005;
long long powm(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1) res = (res * a) % 998244353;
    a = (a * a) % 998244353;
    b >>= 1;
  }
  return res;
}
long long pow(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1) res = (res * a);
    a = (a * a);
    b >>= 1;
  }
  return res;
}
signed main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long q;
  cin >> q;
  while (q--) {
    long long n, k;
    cin >> n >> k;
    vector<long long> a(n);
    for (long long i = 0; i < n; i++) cin >> a[i];
    vector<long long> b(k);
    vector<long long> vis(n + 1, 0);
    for (long long i = 0; i < k; i++) {
      cin >> b[i];
      vis[b[i]] = 1;
    }
    long long f = 1, ans = 1;
    map<long long, long long> m;
    for (long long i = 0; i < n; i++) {
      if (vis[a[i]]) {
        m[a[i]] = i + 1;
      }
    }
    for (long long i = 0; i < k; i++) {
      long long x = m[b[i]];
      if (x + 1 <= n && x - 1 >= 1) {
        if (m[a[x]] && m[a[x - 2]]) {
          f = 0;
        } else if (!m[a[x]] && !m[a[x - 2]]) {
          ans *= 2;
          ans %= 998244353;
        }
        m[a[x - 1]] = 0;
      } else if (x - 1 >= 1) {
        if (m[a[x - 2]]) {
          f = 0;
        }
        m[a[x - 1]] = 0;
      } else if (x + 1 <= n) {
        if (m[a[x]]) {
          f = 0;
        }
        m[a[x - 1]] = 0;
      } else
        f = 0;
    }
    if (!f)
      cout << 0 << '\n';
    else
      cout << ans << '\n';
  }
}
