#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 10, mod = 998244353;
struct node {
  long long l, r, v;
} lst[N];
long long id[N], a[N], b[N], qaq[N];
bool qwq[N];
void del(long long x) {
  lst[lst[x].r].l = lst[x].l;
  lst[lst[x].l].r = lst[x].r;
}
signed main() {
  long long t, n, k;
  scanf("%lld", &t);
  while (t--) {
    scanf("%lld%lld", &n, &k);
    for (long long i = 1; i <= n; i++) qwq[i] = 0, qaq[i] = 0;
    for (long long i = 1; i <= n; i++) {
      scanf("%lld", &a[i]);
      lst[i].l = i - 1;
      lst[i].r = i + 1;
      lst[i].v = a[i];
      id[a[i]] = i;
    }
    lst[0].r = 1;
    lst[n + 1].l = n;
    lst[0].v = lst[n + 1].v = 0;
    for (long long i = 1; i <= k; i++) {
      scanf("%lld", &b[i]);
      qwq[b[i]] = 1;
    }
    qwq[0] = qwq[n + 1] = 1;
    for (long long i = 1; i <= k; i++) {
      long long tmp = id[b[i]];
      qwq[b[i]] = 0;
      if (!qwq[lst[lst[tmp].l].v]) {
        qaq[i]++;
        del(lst[tmp].l);
      }
      if (!qwq[lst[lst[tmp].r].v]) {
        qaq[i]++;
        if (qaq[i] == 1) del(lst[tmp].r);
      }
    }
    long long ans = 1;
    for (long long i = 1; i <= k; i++) ans = (ans * qaq[i]) % mod;
    printf("%d\n", ans);
  }
  return 0;
}
