#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
long long lcm(long long x, long long y) { return x / gcd(x, y) * y; }
long long qpow(long long a, long long b, long long p) {
  a %= p;
  long long ret = 1;
  for (; b; b >>= 1, a = a * a % p)
    if (b & 1) ret = ret * a % p;
  return ret;
}
long long qpow(long long a, long long b) {
  long long ret = 1;
  for (; b; b >>= 1, a = a * a)
    if (b & 1) ret = ret * a;
  return ret;
}
long long getInv(long long x, long long p) { return qpow(x, p - 2, p); }
const int N = 5 + 2e5;
int id[N], val[N], b[N], l[N], r[N];
bool vis[N];
void solve() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    id[x] = i;
    val[i] = x;
    vis[i] = 0;
  }
  val[0] = 0;
  val[n + 1] = n + 1;
  vis[0] = vis[n + 1] = 1;
  for (int i = 1; i <= n; i++) {
    l[val[i]] = val[i - 1];
    r[val[i]] = val[i + 1];
  }
  for (int i = 1; i <= k; i++) {
    cin >> b[i];
    vis[b[i]] = 1;
  }
  int ans = 1;
  for (int i = 1; i <= k; i++) {
    int cnt = 0;
    int v = b[i];
    int x = id[v];
    int left = l[v];
    int right = r[v];
    int leftleft = l[left];
    int rightright = r[right];
    if (!vis[left]) {
      cnt++;
      l[v] = leftleft;
      r[leftleft] = v;
    }
    if (!vis[right]) {
      cnt++;
      r[v] = rightright;
      l[rightright] = v;
    }
    ans = ans * cnt % mod;
    vis[v] = 0;
  }
  cout << ans << endl;
}
int main() {
  int T;
  cin >> T;
  while (T--) solve();
}
