#include <bits/stdc++.h>
using namespace std;
template <typename... Ts>
void do_nothing(Ts &&...vals) {}
constexpr long long MOD = 1000000007;
constexpr int INF = 0x7f7f7f7f;
constexpr double EPS = 1e-8;
long long fastPow(long long num, long long exp) {
  if (exp == 0) return 1;
  if (exp == 1)
    return num;
  else {
    long long half = fastPow(num, exp / 2);
    if (exp % 2 == 1) {
      return half * half * num;
    } else {
      return half * half;
    }
  }
}
int main(int argn, char **argv) {
  ios::sync_with_stdio(false);
  cout.tie(nullptr);
  cin.tie(nullptr);
  int t;
  cin >> t;
  for (int(I) = (0); (I) <= ((t)-1); (I)++) {
    int n, k;
    cin >> n >> k;
    vector<int> a(n + 1), b, pos(n + 1, -1), seq(n + 1, 0);
    for (int(i) = (0); (i) <= ((n)-1); (i)++) {
      cin >> a[i];
      pos[a[i]] = i;
    }
    int temp;
    for (int(i) = (0); (i) <= ((k)-1); (i)++) {
      cin >> temp;
      b.emplace_back(temp);
      seq[pos[temp]] = i + 1;
    }
    int currentCount = 0;
    vector<int> counts, forSim, getSeq(k + 1);
    for (int(i) = (0); (i) <= ((n)-1); (i)++) {
      if (seq[i] == 0) {
        currentCount++;
      } else {
        counts.emplace_back(currentCount);
        currentCount = 0;
        forSim.emplace_back(seq[i]);
        getSeq[seq[i]] = counts.size();
      }
    }
    counts.emplace_back(currentCount);
    long long ans = 1;
    for (int(i) = (1); (i) <= (k); (i)++) {
      int idx = getSeq[i];
      int valid = 0;
      if (counts[idx - 1] > 0) valid++;
      if (counts[idx] > 0) valid++;
      ans = (ans * valid) % 998244353;
      counts[idx - 1] = max(counts[idx], counts[idx - 1]);
      counts[idx] = max(counts[idx], counts[idx - 1]);
    }
    cout << ans << endl;
  }
}
