#include <bits/stdc++.h>
using namespace std;
inline long long rint() {
  long long x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}
inline long long gcd(long long a, long long b) {
  return (b) ? gcd(b, a % b) : a;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long pow(long long a, long long b, long long q) {
  long long rtn = 1;
  while (b) {
    if (b & 1) rtn = rtn * a % q;
    a = a * a % q;
    b >>= 1;
  }
  return rtn;
}
inline long long mysqrt(long long n) {
  long long x = sqrt(n);
  while (x * x < n) x++;
  while (x * x > n) x--;
  return x;
}
const long long mod = 998244353;
long long a[200005];
long long b[200005];
void solve() {
  long long n = rint(), k = rint();
  set<long long> s;
  map<long long, long long> mp;
  for (int i = 1; i <= n; i++) {
    a[i] = rint();
    mp[a[i]] = i;
  }
  for (int i = 1; i <= k; i++) {
    b[i] = rint();
    s.insert(b[i]);
  }
  long long res = 1;
  for (int i = 1; i <= k; i++) {
    s.erase(b[i]);
    long long cnt = 0;
    if (mp[b[i]] - 1 >= 1 && s.find(a[mp[b[i]] - 1]) == s.end()) cnt++;
    if (mp[b[i]] + 1 <= n && s.find(a[mp[b[i]] + 1]) == s.end()) cnt++;
    res = (res * cnt) % mod;
  }
  cout << res << "\n";
}
int main() {
  int t = rint();
  while (t--) solve();
  return 0;
}
