#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long mod = 998244353;
  long long n, k;
  cin >> n >> k;
  long long a[n];
  long long pos[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    pos[a[i]] = i;
  }
  long long b[k];
  set<long long> sig;
  for (int i = 0; i < k; i++) {
    cin >> b[i];
    sig.insert(b[i]);
  }
  long long sol = 1;
  long long cero = 0;
  for (int i = 0; i < k; i++) {
    long long p = pos[b[i]];
    long long mult = 2;
    if (p == 0 or p == n - 1) {
      mult--;
    }
    if (p - 1 >= 0 and sig.count(a[p - 1]) != 0) {
      mult--;
    }
    if (p + 1 < n and sig.count(a[p + 1]) != 0) {
      mult--;
    }
    mult = max(mult, cero);
    sol *= mult;
    sol %= mod;
    sig.erase(b[i]);
  }
  cout << sol << endl;
}
int main() {
  long long t;
  cin >> t;
  while (t--) {
    solve();
  }
}
