#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
void solve() {
  long long n, k;
  cin >> n >> k;
  vector<long long> a(n + 1), b(k), pos(n + 1);
  set<long long> s;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    pos[a[i]] = i;
  }
  for (long long i = 0; i < k; i++) {
    cin >> b[i];
    s.insert(pos[b[i]]);
  }
  long long ans = 1;
  for (long long i = 0; i < k; i++) {
    auto it = s.find(pos[b[i]]);
    long long cnt = 0;
    if (it != s.begin()) {
      if (pos[b[i]] - *(prev(it)) > 1) cnt++;
    } else {
      if (pos[b[i]] > 1) cnt++;
    }
    if (next(it) != s.end()) {
      if (*(next(it)) - pos[b[i]] > 1) {
        cnt++;
      }
    } else {
      if (pos[b[i]] < n) cnt++;
    }
    if (cnt == 0) {
      cout << "0\n";
      return;
    }
    ans = (ans * cnt) % mod;
    s.erase(it);
  }
  cout << ans << "\n";
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long ct;
  cin >> ct;
  while (ct--) solve();
}
