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