#include <bits/stdc++.h>
long long q_pow(long long x, long long n, long long mod) {
  long long ret = 1;
  while (n) {
    if (n & 1) ret = x * ret % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return ret;
}
long long __gcd(long long x, long long y) {
  if (!y) return x;
  return __gcd(y, x % y);
}
long long getbit(long long now) {
  int cnt = 0;
  while (now) {
    cnt++;
    now -= (now) & (-now);
  }
  return cnt;
}
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
using namespace std;
const long long INF = 0x3f3f3f3f;
const long long IINF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 1e6 + 2;
const int maxm = 3e3 + 800;
const double eps = 1e-9;
const int mod = 1 ? 998244353 : 1e9 + 7;
int a[maxn];
int pos[maxn];
int vis[maxn];
int b[maxn];
signed main(int argc, char *argv[]) {
  if (argc == 2 && strcmp("-debug", argv[1]) == 0) {
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
  }
  int t;
  scanf("%d", &t);
  while (t--) {
    int n, k;
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; i++) {
      vis[i] = pos[i] = a[i] = 0;
    }
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
      pos[a[i]] = i;
    }
    for (int i = 1; i <= k; i++) {
      scanf("%d", &b[i]);
      vis[pos[b[i]]] = 1;
    }
    vis[0] = vis[n + 1] = 1;
    long long ans = 1;
    for (int i = 1; i <= k; i++) {
      int now = pos[b[i]];
      int cnt = 2 - vis[now - 1] - vis[now + 1];
      ans = ans * cnt % mod;
      vis[pos[b[i]]] = 0;
    }
    printf("%lld\n", ans);
  }
  return 0;
}
