#include <bits/stdc++.h>
using namespace std;
const long long N = 100005;
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long i, j, k, n, m, ans = 1, cnt = 0, sum = 0;
    cin >> n >> k;
    long long a[n];
    map<long long, long long> mp;
    vector<long long> v(n, 0);
    map<long long, long long> right, left;
    right[-1] = 0;
    left[n] = n - 1;
    for (i = 0; i < n; i++) {
      cin >> a[i];
      mp[a[i]] = i;
      right[i] = i + 1;
      left[i] = i - 1;
    }
    long long b[k];
    for (i = 0; i < k; i++) {
      cin >> b[i];
      v[mp[b[i]]] = 1;
    }
    for (i = 0; i < k; i++) {
      cnt = 0;
      long long x = mp[b[i]];
      if (left[x] >= 0 and v[left[x]] == 0) {
        v[left[x]] = 2;
        right[left[left[x]]] = right[left[x]];
        left[right[left[x]]] = left[left[x]];
        cnt++;
      }
      if (right[x] < n and v[right[x]] == 0) {
        if (cnt == 0) {
          v[right[x]] = 2;
          long long l = left[right[x]];
          long long m = right[x];
          long long r = right[right[x]];
          right[l] = r;
          left[r] = l;
        }
        cnt++;
      }
      ans *= cnt;
      ans %= 998244353;
      v[x] = 0;
    }
    cout << ans << "\n";
  }
  return 0;
}
