#include <bits/stdc++.h>
using namespace std;
int n, k;
int a[200005];
int b[200005];
int idx[200005];
int order[200005];
const int mod = 998244353;
int main() {
  int testcase;
  scanf("%d", &testcase);
  while (testcase--) {
    int n, k;
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
      idx[a[i]] = i;
    }
    for (int i = 1; i <= k; i++) {
      scanf("%d", &b[i]);
      order[b[i]] = i;
    }
    int ans = 1;
    for (int i = 1; i <= k; i++) {
      int cur = idx[b[i]];
      int cnt = 0;
      if (cur > 1 && order[a[cur - 1]] < i) cnt++;
      if (cur < n && order[a[cur + 1]] < i) cnt++;
      ans = ans * cnt % mod;
    }
    for (int i = 1; i <= n; i++) {
      idx[a[i]] = 0;
    }
    for (int i = 1; i <= k; i++) {
      order[b[i]] = 0;
    }
    printf("%d\n", ans);
  }
  return 0;
}
