#include <bits/stdc++.h>
using namespace std;
bool check(long long n, long long pos) { return n & (1LL << pos); }
long long Set(long long n, long long pos) { return n = n | (1LL << pos); }
const long long mod = 998244353;
long long a[200005];
long long b[200005];
set<long long> st;
map<long long, long long> mp;
struct BIT {
  vector<long long> tree;
  long long n;
  BIT(long long n) : n(n), tree(n + 3, 0) {}
  void init() { tree.assign(n + 3, 0); }
  long long query(long long idx) {
    long long sum = 0;
    while (idx > 0) {
      sum += tree[idx];
      idx -= idx & (-idx);
    }
    return sum;
  }
  void update(long long idx, long long x) {
    while (idx <= n) {
      tree[idx] += x;
      idx += idx & (-idx);
    }
  }
};
long long solve(long long n, long long k) {
  mp.clear();
  st.clear();
  for (long long i = 1; i <= n; i++) mp[a[i]] = i;
  for (long long i = 1; i <= k; i++) st.insert(mp[b[i]]);
  BIT bit(n);
  bit.init();
  long long ret = 1;
  st.insert(n + 1);
  st.insert(0);
  for (long long i = 1; i <= k; i++) {
    long long pos = mp[b[i]];
    st.erase(st.find(pos));
    auto it = st.upper_bound(pos);
    long long daan = *it - 1;
    it--;
    long long baam = *it;
    baam = baam + 1;
    long long daane = (daan - pos) - bit.query(daan) + bit.query(pos);
    long long baame =
        (pos - baam) - bit.query(pos) + (baam > 1 ? bit.query(baam - 1) : 0);
    if (daane && baame)
      ret = (ret * 2LL) % mod;
    else if (daane == 0 && baame == 0)
      return 0;
    bit.update(pos, 1);
  }
  return ret;
}
int main() {
  long long i, j, k, l, m, n, o, r, q;
  long long testcase;
  long long input, flag, tag, ans;
  scanf("%lld", &testcase);
  for (long long cs = 1; cs <= testcase; cs++) {
    cin >> n >> k;
    for (long long i = 1; i <= n; i++) cin >> a[i];
    for (long long i = 1; i <= k; i++) cin >> b[i];
    ans = solve(n, k);
    cout << ans << "\n";
  }
}
