#include <bits/stdc++.h>
using namespace std;
long double PI = 3.14159265359;
long double DEL = 1e-10;
int mod = 998244353;
long long fpow(long long x, long long n) {
  long long res = 1;
  while (n) {
    if (n & 1) res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
void sieve(long long n) {
  bool prime[1000006];
  memset(prime, true, sizeof(prime));
  long long rootn = (long long)sqrt(n);
  for (long long p = 2; p <= rootn; p++)
    if (prime[p] == true)
      for (long long i = p * p; i <= n; i += p) prime[i] = false;
  prime[1] = 0;
}
const long long N = 300050;
long long cnt, sum, mid, mx, mn, ans, a[N], b[N];
long long n, m, d, i, j, k, l, p, q, r, t, w, x, y, z;
bool f, f1, f2;
string s;
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  for (long long tt = 1; tt <= t; ++tt) {
    cin >> n >> k;
    vector<long long> l(n + 3), r(n + 3), isp(n + 3), idx(n + 3);
    for (long long i = 1; i <= n; i++) {
      cin >> a[i];
      l[i] = i - 1;
      r[i] = i + 1;
      idx[a[i]] = i;
    }
    for (long long i = 1; i <= k; i++) {
      cin >> b[i];
      isp[b[i]] = 1;
    }
    ans = 1;
    for (long long i = 1; i <= k; i++) {
      long long left = 0, right = 0;
      long long onright = r[idx[b[i]]], onleft = l[idx[b[i]]], curr = idx[b[i]];
      isp[b[i]] = 0;
      if (onleft > 0 and !isp[a[onleft]]) {
        left++;
      }
      if (onright <= n and !isp[a[onright]]) {
        right++;
      }
      ans = ans * (left + right);
      if (ans == 0) {
        break;
      }
      ans %= mod;
      if (left) {
        r[l[onleft]] = curr;
        l[curr] = l[onleft];
      } else {
        l[r[onright]] = curr;
        r[curr] = r[onright];
      }
    }
    cout << ans;
    cout << "\n";
  }
}
