#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const long long N = 200005;
int dx[] = {-1, 0, 1, 0, 1, 1, -1, -1};
int dy[] = {0, -1, 0, 1, 1, -1, 1, -1};
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
long long powb(long long a, long long b) {
  long long res = 1;
  a;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a;
    a = a * a;
  }
  return res;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
void test() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n), b(k), inda(n), vis(n);
  set<pair<int, int> > s;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
    inda[a[i]] = i;
    s.insert({i, a[i]});
  }
  for (int i = 0; i < k; i++) {
    cin >> b[i];
    b[i]--;
    vis[b[i]] = 1;
  }
  long long ans = 1;
  for (int i = 0; i < k; i++) {
    int idx = inda[b[i]];
    auto pos = s.find({idx, a[idx]});
    int inc = 0;
    if (pos != s.begin()) {
      if (vis[(*(prev(pos))).second] == 0) {
        inc++;
      }
    }
    if (next(pos) != s.end()) {
      if (vis[(*(next(pos))).second] == 0) inc++;
    }
    (ans *= 1LL * inc) %= mod;
    vis[b[i]] = 0;
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  cin >> t;
  while (t--) {
    test();
  }
}
