#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int inf = (1 << 30) - 1;
const ll INF = (1LL << 60) - 1;
const double pi = acos(-1.0);
const double EPS = 1e-10;
template <typename T>
bool chmax(T &x, const T &y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T &x, const T &y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}
void in() {}
template <typename Head, typename... Tail>
void in(Head &&head, Tail &&...tail) {
  cin >> head;
  in(forward<Tail>(tail)...);
}
void out() { cout << '\n'; }
template <typename Head, typename... Tail>
void out(Head &&head, Tail &&...tail) {
  cout << head << ' ';
  out(forward<Tail>(tail)...);
}
void outn() {}
template <typename Head, typename... Tail>
void outn(Head &&head, Tail &&...tail) {
  cout << head << '\n';
  outn(forward<Tail>(tail)...);
}
template <typename T, typename U>
void in(pair<T, U> &p) {
  cin >> p.first >> p.second;
}
template <typename T, typename U>
void out(pair<T, U> p) {
  cout << p.first << ' ' << p.second << '\n';
}
using vi = vector<int>;
using vll = vector<ll>;
template <typename T>
void vin(vector<T> &a) {
  for (int i = 0; i < (int)a.size(); i++) cin >> a[i];
}
template <typename T>
void vout(const vector<T> &a) {
  for (auto &e : a) cout << e << ' ';
  cout << '\n';
}
template <typename T>
void voutn(const vector<T> &a) {
  for (auto &e : a) cout << e << '\n';
}
template <typename T>
void unique(vector<T> &a) {
  sort(a.begin(), a.end()), a.erase(unique(a.begin(), a.end()), a.end());
}
vector<int> iota(int n) {
  vector<int> ret(n);
  iota(ret.begin(), ret.end(), 0);
  return ret;
}
template <typename T>
vector<int> iota(const vector<T> &a, bool greater = false) {
  vector<int> ret = iota((int)a.size());
  sort(ret.begin(), ret.end(),
       [&](int i, int j) { return (a[i] < a[j]) ^ greater; });
  return ret;
}
struct io_setup {
  io_setup() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout << fixed << setprecision(15);
  }
} io_setup;
const int MOD = 998244353;
template <int mod>
struct Mod_Int {
  int x;
  Mod_Int() : x(0) {}
  Mod_Int(ll y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
  Mod_Int &operator+=(const Mod_Int &p) {
    if ((x += p.x) >= mod) x -= mod;
    return *this;
  }
  Mod_Int &operator-=(const Mod_Int &p) {
    if ((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
  Mod_Int &operator*=(const Mod_Int &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }
  Mod_Int &operator/=(const Mod_Int &p) {
    *this *= p.inverse();
    return *this;
  }
  Mod_Int &operator++() { return *this += Mod_Int(1); }
  Mod_Int operator++(int) {
    Mod_Int tmp = *this;
    ++*this;
    return tmp;
  }
  Mod_Int &operator--() { return *this -= Mod_Int(1); }
  Mod_Int operator--(int) {
    Mod_Int tmp = *this;
    --*this;
    return tmp;
  }
  Mod_Int operator-() const { return Mod_Int(-x); }
  Mod_Int operator+(const Mod_Int &p) const { return Mod_Int(*this) += p; }
  Mod_Int operator-(const Mod_Int &p) const { return Mod_Int(*this) -= p; }
  Mod_Int operator*(const Mod_Int &p) const { return Mod_Int(*this) *= p; }
  Mod_Int operator/(const Mod_Int &p) const { return Mod_Int(*this) /= p; }
  bool operator==(const Mod_Int &p) const { return x == p.x; }
  bool operator!=(const Mod_Int &p) const { return x != p.x; }
  Mod_Int inverse() const {
    assert(*this != Mod_Int(0));
    return pow(mod - 2);
  }
  Mod_Int pow(ll k) const {
    Mod_Int now = *this, ret = 1;
    for (; k; k >>= 1, now *= now) {
      if (k & 1) ret *= now;
    }
    return ret;
  }
  friend ostream &operator<<(ostream &os, const Mod_Int &p) {
    return os << p.x;
  }
  friend istream &operator>>(istream &is, Mod_Int &p) {
    ll a;
    is >> a;
    p = Mod_Int<mod>(a);
    return is;
  }
};
template <int mod>
struct Combination {
  using Mint = Mod_Int<mod>;
  vector<Mint> _fac, _ifac;
  Combination(int n) {
    _fac.resize(n + 1), _ifac.resize(n + 1);
    _fac[0] = 1;
    for (int i = 1; i <= n; i++) _fac[i] = _fac[i - 1] * i;
    _ifac[n] = _fac[n].inverse();
    for (int i = n; i >= 1; i--) _ifac[i - 1] = _ifac[i] * i;
  }
  Mint fac(int k) const { return _fac[k]; }
  Mint ifac(int k) const { return _ifac[k]; }
  Mint comb(int n, int k) const {
    if (k < 0 || n < k) return 0;
    return fac(n) * ifac(n - k) * ifac(k);
  }
  Mint perm(int n, int k) const {
    if (k < 0 || n < k) return 0;
    return fac(n) * ifac(n - k);
  }
  Mint second_stirling_number(ll n, int k) {
    Mint ret = 0;
    for (int i = 0; i < k + 1; i++) {
      Mint tmp = comb(k, i) * Mint(k - i).pow(n);
      ret += ((i & 1) ? -tmp : tmp);
    }
    return ret * ifac(k);
  }
};
using mint = Mod_Int<MOD>;
using comb = Combination<MOD>;
int main() {
  int T;
  in(T);
  while (T--) {
    int N, K;
    in(N, K);
    vector<int> a(N);
    vin(a);
    vector<int> b(K);
    vin(b);
    for (auto &e : a) e--;
    for (auto &e : b) e--;
    vi id(N, -1);
    for (int i = 0; i < K; i++) id[b[i]] = i;
    int n = 0;
    mint ans = 1;
    for (int i = 0; i < N; i++) {
      if (id[a[i]] == -1) continue;
      mint tmp = 0;
      if (i > 0 && id[a[i - 1]] < id[a[i]]) tmp++;
      if (i < N - 1 && id[a[i]] > id[a[i + 1]]) tmp++;
      ans *= tmp;
    }
    out(ans);
  }
}
