#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int T;
  cin >> T;
  for (int cass = 1; cass <= T; ++cass) {
    int n, k;
    cin >> n >> k;
    vector<int> pos(n + 1), b(k);
    vector<bool> vis(n + 1);
    for (int i = 0; i < n; ++i) {
      int x;
      cin >> x;
      pos[x] = i + 1;
    }
    for (int i = 0; i < k; ++i) {
      int x;
      cin >> x;
      b[i] = pos[x];
      vis[b[i]] = 1;
    }
    set<int> s{-1, n + 1};
    for (int i = 1; i <= n; ++i) {
      s.insert(i);
    }
    long long ans = 1;
    for (int i = 0; i < k; ++i) {
      auto it = s.lower_bound(b[i]);
      int l = *prev(it), r = *next(it);
      bool gl = l >= 1 && !vis[l];
      bool gr = r <= n && !vis[r];
      if (!gl && !gr) {
        ans = 0;
      } else if (gl && gr) {
        ans <<= 1;
        ans %= mod;
        s.erase(l);
      } else if (gl) {
        s.erase(l);
      } else {
        s.erase(r);
      }
      vis[b[i]] = 0;
    }
    cout << ans << '\n';
  }
  return 0;
}
