#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 10;
const long long MOD = 998244353;
int a[MAX], b[MAX], pos[MAX];
bool vis[MAX];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
      vis[i] = false;
      cin >> a[i];
      pos[a[i]] = i;
    }
    vis[0] = vis[n + 1] = true;
    for (int i = 1; i <= k; i++) {
      cin >> b[i];
      vis[pos[b[i]]] = true;
    }
    long long ans = 1;
    for (int i = 1; i <= k; i++) {
      int cnt = !vis[pos[b[i]] - 1] + !vis[pos[b[i]] + 1];
      ans = (ans * cnt) % MOD;
      vis[pos[b[i]]] = false;
    }
    cout << ans << "\n";
  }
  return 0;
}
