#include <bits/stdc++.h>
using namespace std;
void testcase() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < (n); i++) {
    cin >> a[i];
    a[i]--;
  }
  vector<int> b(k);
  for (int i = 0; i < (k); i++) {
    cin >> b[i];
    b[i]--;
  }
  vector<int> prior(n, -1);
  vector<int> inv_a(n);
  for (int i = 0; i < (n); i++) inv_a[a[i]] = i;
  for (int i = 0; i < (k); i++) prior[inv_a[b[i]]] = i;
  vector<bool> canLeft(n, true);
  vector<bool> canRight(n, true);
  int bs_encountered = 0;
  stack<tuple<int, int, int>> stk;
  for (int i = 0; i < (n); i++) {
    if (prior[i] == -1) {
    } else {
      while (!stk.empty() && get<1>(stk.top()) < prior[i]) stk.pop();
      if (stk.empty()) {
        if (i == 0) canLeft[i] = false;
      } else {
        int pos, val, delta;
        tie(pos, val, delta) = stk.top();
        if (abs(i - pos) - (bs_encountered - delta) <= 1) canLeft[i] = false;
      }
      bs_encountered++;
      stk.push({i, prior[i], bs_encountered});
    }
  }
  bs_encountered = 0;
  while (!stk.empty()) stk.pop();
  for (int i = n - 1; i >= 0; i--) {
    if (prior[i] == -1) {
    } else {
      while (!stk.empty() && get<1>(stk.top()) < prior[i]) stk.pop();
      if (stk.empty()) {
        if (i == n - 1) canRight[i] = false;
      } else {
        int pos, val, delta;
        tie(pos, val, delta) = stk.top();
        if (abs(i - pos) - (bs_encountered - delta) <= 1) canRight[i] = false;
      }
      bs_encountered++;
      stk.push({i, prior[i], bs_encountered});
    }
  }
  long long int result = 1;
  for (int i = 0; i < (n); i++) {
    if (prior[i] != -1) {
      int options = ((canLeft[i]) ? 1 : 0) + (canRight[i] ? 1 : 0);
      result *= options;
    }
    result = result % 998244353;
  }
  cout << result << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int T;
  cin >> T;
  for (int tc = 0; tc < (T); tc++) testcase();
  return 0;
}
