#include <bits/stdc++.h>
const int MOD = 998244353;
int main() {
  int t;
  std::cin >> t;
  while (t--) {
    int n, k;
    std::cin >> n >> k;
    std::list<int> a;
    std::vector<int> b(k);
    for (int i = 0; i < n; i++) {
      int v;
      std::cin >> v;
      a.push_back(v);
    }
    for (int i = 0; i < k; i++) std::cin >> b[i];
    std::vector<std::list<int>::iterator> its(n + 1);
    for (std::list<int>::iterator it = a.begin(); it != a.end(); it++) {
      its[*it] = it;
    }
    std::set<int> inB;
    for (const auto v : b) inB.insert(v);
    long long ans = 1;
    for (int i = 0; i < k; i++) {
      const int target = b[i];
      const auto targetIt = its[target];
      std::list<int>::iterator leftIt, rightIt;
      bool canRemoveLeft = false;
      bool canRemoveRight = false;
      if (targetIt != a.begin()) {
        leftIt = std::prev(targetIt);
        if (inB.find(*leftIt) == inB.end()) canRemoveLeft = true;
      }
      if (std::next(targetIt) != a.end()) {
        rightIt = std::next(targetIt);
        if (inB.find(*rightIt) == inB.end()) canRemoveRight = true;
      }
      if (canRemoveLeft && canRemoveRight) {
        ans <<= 1;
        ans %= MOD;
      }
      if (!canRemoveLeft && !canRemoveRight) {
        ans = 0;
        break;
      }
      inB.erase(target);
      if (canRemoveLeft)
        a.erase(leftIt);
      else
        a.erase(rightIt);
    }
    std::cout << ans << std::endl;
  }
  return 0;
}
