#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int &x : a) cin >> x;
  vector<int> b(m);
  for (int &x : b) cin >> x;
  map<int, int> posA;
  for (int i = 0; i < n; i++) posA[a[i]] = i + 1;
  map<int, int> posB;
  for (int i = 0; i < m; i++) posB[b[i]] = i + 1;
  int ans = 1;
  for (int i = 0; i < m; i++) {
    int x = b[i];
    if (posA[x] == 1) {
      int y = a[1];
      if (posB[y] > i + 1) ans = 0;
    } else if (posA[x] == n) {
      int y = a[n - 2];
      if (posB[y] > i + 1) ans = 0;
    } else {
      int p = a[posA[x] - 2];
      int q = a[posA[x]];
      if (posB[p] > i + 1 && posB[q] > i + 1) ans = 0;
      if (posB[p] <= i + 1 && posB[q] <= i + 1) ans += ans;
    }
    if (ans >= mod) ans -= mod;
  }
  cout << ans << "\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int testcase = 1;
  cin >> testcase;
  for (int i = 1; i <= testcase; i++) {
    solve();
  }
  return 0;
}
