#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
using namespace std;
const int N = 200003, mod = 998244353;
const long long inf = (1e18 + 11);
int a[N], b[N], ind[N];
bool vis[N];
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int tc;
  cin >> tc;
  while (tc--) {
    int n, k;
    cin >> n >> k;
    int i, j;
    fill(vis, vis + n + 1, 0);
    for (i = 1; i <= n; ++i) cin >> a[i], ind[a[i]] = i;
    for (i = 1; i <= k; ++i) cin >> b[i], vis[b[i]] = 1;
    int ans = 1;
    for (i = 1; i <= k; ++i) {
      j = 0;
      if (ind[b[i]] >= 2 and !vis[a[ind[b[i]] - 1]]) ++j;
      if (ind[b[i]] <= n - 1 and !vis[a[ind[b[i]] + 1]]) ++j;
      ans *= j, ans %= mod;
      vis[b[i]] = 0;
    }
    cout << ans << '\n';
  }
  return 0;
}
