#include <bits/stdc++.h>
using namespace std;
int T, n, m, i, j, k, a[200005], b[200005], c[200005], bz[200005];
long long ans;
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d%d", &n, &m);
    for (i = 1; i <= n; i++) scanf("%d", &a[i]), b[a[i]] = i, bz[i] = 0;
    for (i = 1; i <= m; i++) scanf("%d", &c[i]), bz[c[i]] = i;
    ans = 1;
    for (i = 1; i <= m; i++) {
      int x = b[c[i]];
      long long ans0 = 0;
      if (x > 1 && bz[a[x - 1]] <= i) ans0 += ans;
      if (x < n && bz[a[x + 1]] <= i) ans0 += ans;
      ans = ans0 % 998244353;
    }
    printf("%lld\n", ans);
  }
}
