#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "inline", "-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
using namespace std;
const long long inf = 1e9;
const long long onf = 1e18;
const long long maxn = 2e5 + 10;
const long long mod = 998244353;
inline long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 3) + (x << 1) + ch - 48;
    ch = getchar();
  }
  return x * f;
}
long long a[maxn], b[maxn], p[maxn], vis[maxn];
inline void cf() {
  long long t = read();
  while (t--) {
    long long n, m;
    cin >> n >> m;
    for (long long i = 0; i < n; i++) vis[i] = p[i] = 0;
    for (long long i = 0; i < n; i++) cin >> a[i], a[i]--, p[a[i]] = i;
    for (long long i = 0; i < m; i++) cin >> b[i], b[i]--, vis[b[i]] = 1;
    long long ans = 1;
    for (long long i = 0; i < m; i++) {
      long long pos = p[b[i]], cnt = 0;
      if (pos - 1 >= 0 && !vis[a[pos - 1]]) cnt++;
      if (pos + 1 < n && !vis[a[pos + 1]]) cnt++;
      ans = ans * cnt % mod;
      vis[b[i]] = 0;
    }
    cout << ans << endl;
  }
  return;
}
signed main() {
  cf();
  return 0;
}
