#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
using namespace std;
const int N = 2e5 + 10;
int t, n, k, a[N], b[N], vis[N], pos[N];
const int mod = 998244353;
int main() {
  ios::sync_with_stdio(false);
  for (cin >> t; t; t--) {
    cin >> n >> k;
    for (int i = 1; i <= n; i++) cin >> a[i], vis[i] = 0, pos[a[i]] = i;
    for (int i = 1; i <= k; i++) cin >> b[i], vis[b[i]] = 1;
    int ans = 1;
    for (int i = 1; i <= k; i++) {
      int now = pos[b[i]];
      int res = 0;
      if (now > 1 && !vis[a[now - 1]]) res++;
      if (now < n && !vis[a[now + 1]]) res++;
      ans = 1ll * ans * res % mod;
      vis[b[i]] = 0;
    }
    cout << ans << '\n';
  }
  return 0;
}
