#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int N = 2e5 + 5;
int A[N], B[N], pos[N];
bool st[N];
void solve() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    pos[A[i]] = i;
  }
  for (int i = 0; i < k; i++) {
    cin >> B[i];
    st[B[i]] = true;
  }
  long long ans = 1;
  for (int i = 0; i < k; i++) {
    st[B[i]] = false;
    int t = 0;
    t += (pos[B[i]] - 1 >= 0) && !st[A[pos[B[i]] - 1]];
    t += (pos[B[i]] + 1 < n) && !st[A[pos[B[i]] + 1]];
    ans = ans * (long long)t % mod;
  }
  cout << ans << "\n";
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int test;
  cin >> test;
  while (test--) solve();
}
