#include <bits/stdc++.h>
const long long N = 2e5 + 5, mod = 998244353;
int n, m, A[N], B[N], p[N];
long long ans;
bool vis[N];
int main() {
  register int t;
  scanf("%d", &t);
  while (t--) {
    scanf("%d%d", &n, &m);
    for (register int i = 1; i <= n; i++) p[i] = vis[i] = 0;
    for (register int i = 1; i <= n; i++) scanf("%d", A + i), p[A[i]] = i;
    for (register int i = 1; i <= m; i++) scanf("%d", B + i), vis[B[i]] = 1;
    ans = 1;
    for (register int i = 1; i <= m; i++) {
      register int pos = p[B[i]], c = 0;
      if (pos - 1 >= 1 && !vis[A[pos - 1]]) c++;
      if (pos + 1 <= n && !vis[A[pos + 1]]) c++;
      ans = ans * c % mod;
      vis[B[i]] = 0;
    }
    printf("%lld\n", ans);
  }
  return 0;
}
