#include <bits/stdc++.h>
using namespace std;
const int N = 500050;
const int inf = 0x3f3f3f3f;
const long long llinf = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const double PI = acos(-1.0);
clock_t TIME__START, TIME__END;
void program_end() {}
int n, k;
int a[N], b[N], pos[N], c[N];
inline void solve() {
  long long ans = 1;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), c[i] = 1, pos[a[i]] = i;
  for (int i = 1; i <= k; ++i) scanf("%d", &b[i]), c[pos[b[i]]] = 0;
  for (int i = 1; i <= k; ++i) {
    long long fac = 0;
    fac += ((pos[b[i]] > 1) && (c[pos[b[i]] - 1])) +
           ((pos[b[i]] < n) && (c[pos[b[i]] + 1]));
    c[pos[b[i]]] = 1;
    ans = (ans * fac) % mod;
  }
  cout << ans << '\n';
}
int main() {
  TIME__START = clock();
  int Test = 1;
  scanf("%d", &Test);
  while (Test--) {
    solve();
  }
  TIME__END = clock();
  program_end();
  return 0;
}
