#include <bits/stdc++.h>
using namespace std;
void read(long long &x) {
  char ch = getchar();
  x = 0;
  while (!isdigit(ch)) ch = getchar();
  while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();
}
const long long N = 2e5 + 5, mod = 998244353;
long long n, m, a[N], b[N], t[N], w[N];
signed main() {
  long long T;
  read(T);
  while (T--) {
    read(n), read(m);
    long long res = 1;
    for (long long i = 1; i <= n; ++i) read(a[i]), w[a[i]] = i;
    for (long long i = 1; i <= n; ++i) t[i] = 0;
    for (long long i = 1; i <= m; ++i) read(b[i]), t[b[i]] = i;
    for (long long i = 1; i <= m; ++i) {
      long long x = w[b[i]];
      long long c = (x != 1 && t[a[x - 1]] < i) + (x != n && t[a[x + 1]] < i);
      res = res * c % mod;
    }
    printf("%lld\n", res);
  }
  return 0;
}
