#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000 + 5;
const int N = 2e5 + 5;
const long long oo = 1e18 + 5;
const long long mod = 998244353;
int a[N], b[N];
int mul(int a, int b) { return 1ll * a * b % mod; }
void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> pos(n + 2);
  vector<int> lv(n + 2);
  for (int i = 1; i <= n; ++i) {
    int x;
    cin >> x;
    a[i] = x;
    pos[x] = i;
    lv[i] = 0;
  }
  for (int i = 1; i <= k; ++i) {
    int x;
    cin >> x;
    b[i] = x;
    lv[pos[x]] = i;
  }
  lv[0] = lv[n + 1] = INF;
  int ans = 1;
  for (int i = 1; i <= n; ++i) {
    if (lv[i]) {
      if (lv[i] > lv[i - 1] && lv[i] > lv[i + 1])
        ans = mul(ans, 2);
      else {
        if (lv[i] <= lv[i - 1] && lv[i] <= lv[i + 1]) {
          cout << 0 << '\n';
          return;
        }
      }
    }
  }
  cout << ans << '\n';
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
