#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
long long int power(long long int x, long long int y,
                    long long int p = 1000000007) {
  long long int res = 1LL;
  x = x % p;
  if (x == 0) return 0;
  while (y > 0) {
    if (y & 1LL) res = (res * x) % p;
    y = y >> 1LL;
    x = (x * x) % p;
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    list<long long int> a;
    vector<bool> needed(n + 1, false);
    vector<list<long long int>::iterator> pointer(n + 1);
    vector<long long int> b;
    long long int val;
    for (int i = 0; i < n; ++i) {
      cin >> val;
      a.push_back(val);
      pointer[val] = prev(a.end());
    }
    for (int i = 0; i < k; ++i) {
      cin >> val;
      b.push_back(val);
      needed[val] = true;
    }
    long long int prod = 1LL;
    for (int i = 0; i < k; ++i) {
      long long int mult = 0LL;
      auto curr = pointer[b[i]];
      auto nxt = next(curr);
      if (nxt != a.end() && !needed[*nxt]) {
        ++mult;
        needed[*nxt] = false;
      }
      if (curr != a.begin()) {
        auto pre = prev(curr);
        if (!needed[*pre]) {
          ++mult;
          needed[*pre] = false;
        }
      }
      prod = (prod * mult) % 998244353;
      if (prod == 0LL) break;
      a.erase(curr);
    }
    cout << prod << endl;
  }
  return 0;
}
