#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200010, P = 998244353;
int t, n, k, ans, a[MAXN], b[MAXN], vis[MAXN], pre[MAXN], nx[MAXN];
int main() {
  scanf("%d", &t);
  for (int ii = 1; ii <= t; ii++) {
    scanf("%d%d", &n, &k);
    pre[0] = nx[0] = 0;
    for (int i = 1; i <= n; i++) {
      pre[i] = nx[i] = vis[i] = 0;
    }
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
      if (i != 1) {
        nx[a[i - 1]] = a[i], pre[a[i]] = a[i - 1];
      }
    }
    for (int i = 1; i <= k; i++) {
      scanf("%d", &b[i]);
      vis[b[i]] = 1;
    }
    ans = 1;
    for (int i = 1; i <= k; i++) {
      vis[b[i]] = 0;
      int aa = pre[b[i]], bb = nx[b[i]];
      int flga = (aa == 0 || vis[aa]), flgb = (bb == 0 || vis[bb]);
      ans = (ans * (2 - flga - flgb)) % P;
      nx[aa] = bb, pre[bb] = aa;
      pre[0] = nx[0] = 0;
    }
    printf("%d\n", ans);
  }
  return 0;
}
