#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int maxn = 2e5 + 7;
int a[maxn], b[maxn];
int las[maxn], nex[maxn], vis[maxn];
void link(int x, int y) {
  nex[x] = y;
  las[y] = x;
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    long long ans = 1;
    int n, k;
    scanf("%d%d", &n, &k);
    a[n + 1] = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
      vis[i] = 0;
    }
    for (int i = 1; i <= n; i++) {
      las[a[i]] = a[i - 1];
      nex[a[i]] = a[i + 1];
    }
    for (int i = 1; i <= k; i++) {
      scanf("%d", &b[i]);
      vis[b[i]] = 1;
    }
    vis[0] = 1;
    for (int i = 1; i <= k; i++) {
      int cnt = 0;
      int flag = 0;
      if (!vis[nex[b[i]]]) cnt++, flag = 1;
      if (!vis[las[b[i]]]) cnt++, flag = 2;
      if (!flag) {
        ans = 0;
        break;
      } else if (flag == 1) {
        link(b[i], nex[nex[b[i]]]);
      } else if (flag == 2) {
        link(las[las[b[i]]], b[i]);
      }
      vis[b[i]] = 0;
      ans = ans * cnt % mod;
    }
    printf("%lld\n", ans);
  }
  return 0;
}
