#include <bits/stdc++.h>
using namespace std;
const int N = 200000 + 100;
int a[N], b[N];
const int mod = 998244353;
bool vt[N];
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n, k;
    scanf("%d%d", &n, &k);
    for (int i = 0; i < n; i++) {
      int x;
      scanf("%d", &x);
      a[x] = i + 1;
    }
    for (int i = 0; i < k; i++) {
      scanf("%d", b + i);
    }
    vt[0] = 0;
    vt[n + 1] = 0;
    for (int i = 1; i <= n; i++) vt[i] = 1;
    int ans = 1;
    for (int i = k - 1; i >= 0; i--) {
      int cnt = vt[a[b[i]] - 1] + vt[a[b[i]] + 1];
      ans = ans * cnt % mod;
      vt[a[b[i]]] = 0;
    }
    printf("%d\n", ans);
  }
}
