#include <bits/stdc++.h>
using namespace std;
long long M = 998244353;
int a[1000000];
int b[1000000];
int invb[1000000];
void testCase() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < k; i++) cin >> b[i];
  for (int i = 0; i <= n; i++) invb[i] = -1;
  for (int i = 0; i < k; i++) invb[b[i]] = i;
  long long ans = 1;
  for (int i = 0; i < n; i++) {
    if (invb[a[i]] == -1) continue;
    if (i == 0) {
      int choices = 0;
      if (invb[a[i + 1]] < invb[a[i]]) choices++;
      ans *= choices;
      ans %= M;
    } else if (i == n - 1) {
      int choices = 0;
      if (invb[a[i - 1]] < invb[a[i]]) choices++;
      ans *= choices;
      ans %= M;
    } else {
      int choices = 0;
      if (invb[a[i - 1]] < invb[a[i]]) choices++;
      if (invb[a[i + 1]] < invb[a[i]]) choices++;
      ans *= choices;
      ans %= M;
    }
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    testCase();
  }
}
