#include <bits/stdc++.h>
using namespace std;
const int N = 2e5;
int a[N + 10], b[N + 10], t[N + 10], pos[N + 10];
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    memset(t, -1, sizeof(t));
    int n, m;
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
      pos[a[i]] = i;
    }
    for (int i = 1; i <= m; i++) {
      scanf("%d", &b[i]);
      t[pos[b[i]]] = i;
    }
    int ans = 1;
    for (int i = 1; i <= m; i++) {
      int cnt = 0;
      if (pos[b[i]] > 1 && t[pos[b[i]] - 1] < t[pos[b[i]]]) cnt++;
      if (pos[b[i]] < n && t[pos[b[i]] + 1] < t[pos[b[i]]]) cnt++;
      ans *= cnt;
      ans %= 998244353;
    }
    printf("%d\n", ans);
  }
  return 0;
}
