#include <bits/stdc++.h>
using namespace std;
long long ans = 0;
int n, k;
int arr[200001];
int pos[200001];
long long mod = 998244353;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long t = 1;
  cin >> t;
  while (t--) {
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
      cin >> arr[i];
      pos[i] = -1;
    }
    pos[n] = -1;
    int temp;
    for (int i = 0; i < k; i++) {
      cin >> temp;
      pos[temp] = i;
    }
    ans = 1;
    for (int i = 0; i < n; i++) {
      temp = arr[i];
      if (pos[temp] == -1) continue;
      int possi = 0;
      if ((i - 1) >= 0 && pos[arr[i - 1]] < pos[temp]) possi++;
      if ((i + 1) < n && pos[arr[i + 1]] < pos[temp]) possi++;
      ans *= possi;
      ans %= mod;
    }
    cout << ans << "\n";
  }
  return 0;
}
