#include <bits/stdc++.h>
using namespace std;
long long int a[200009], p[200009], b[200009];
void solve() {
  long long int n, m;
  cin >> n >> m;
  for (long long int i = 1; i <= n; i++) {
    cin >> a[i];
    p[a[i]] = i;
  }
  a[0] = -1;
  a[n + 1] = -1;
  set<long long int> s;
  for (long long int i = 0; i < m; i++) {
    cin >> b[i];
    s.insert(b[i]);
  }
  long long int ans = 1;
  long long int mod1 = 998244353;
  s.insert(-1);
  for (long long int i = 0; i < m; i++) {
    long long int x = b[i];
    long long int y = 0;
    long long int pos = p[x];
    if (s.find(a[pos - 1]) == s.end()) y++;
    if (s.find(a[pos + 1]) == s.end()) y++;
    s.erase(s.find(x));
    ans *= y;
    ans %= mod1;
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int begtime = clock();
  long long int t = 1;
  cin >> t;
  for (long long int i = 0; i < t; i++) {
    solve();
  }
  int endtime = clock();
  cerr << "\n\n"
       << "Time elapsed: " << (endtime - begtime) * 1000 / CLOCKS_PER_SEC
       << " ms\n\n";
  return 0;
}
