#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5, mod = 998244353;
int n, k, a[N], b[N], p[N];
bool vis[N];
void solve() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    p[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 x = p[b[i]];
    int cur = 0;
    if (x - 1 >= 1) cur += !vis[a[x - 1]];
    if (x + 1 <= n) cur += !vis[a[x + 1]];
    ans = 1ll * ans * cur % mod;
    vis[b[i]] = 0;
  }
  cout << ans << '\n';
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int _;
  cin >> _;
  while (_--) solve();
  return 0;
}
