#include <bits/stdc++.h>
inline int Max(int x, int y) { return x > y ? x : y; }
inline int Min(int x, int y) { return x < y ? x : y; }
inline int read() {
  int r = 0;
  bool w = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') w = ch == '-' ? 1 : w, ch = getchar();
  while (ch >= '0' && ch <= '9')
    r = (r << 3) + (r << 1) + (ch ^ 48), ch = getchar();
  return w ? ~r + 1 : r;
}
const int N = 200010;
const long long mod = 998244353;
long long ans;
int n, m, a[N], b[N];
int l[N], r[N], vis[N], head = 0, tail = N - 1;
bool tag[N];
signed main() {
  int T = read();
  while (T--) {
    n = read();
    m = read();
    ans = 1;
    for (int i = 1; i <= n; ++i) a[i] = read(), vis[a[i]] = i, tag[i] = 0;
    for (int i = 2; i < n; ++i) l[i] = i - 1, r[i] = i + 1;
    l[1] = head;
    r[1] = 2;
    l[n] = n - 1;
    r[n] = tail;
    tag[head] = tag[tail] = 1;
    for (int i = 1; i <= m; ++i) {
      b[i] = read();
      tag[vis[b[i]]] = 1;
    }
    for (int i = 1; i <= m; ++i) {
      int x = b[i], cnt = 0;
      if (!tag[l[vis[x]]]) ++cnt;
      if (!tag[r[vis[x]]]) ++cnt;
      ans = ans * cnt % mod;
      tag[vis[x]] = 0;
      r[l[vis[x]]] = r[vis[x]];
      l[r[vis[x]]] = l[vis[x]];
    }
    printf("%lld\n", ans);
  }
  return 0;
}
