#include <bits/stdc++.h>
using namespace std;
inline long long max_(long long a, long long b) { return a > b ? a : b; }
inline long long min_(long long a, long long b) { return a < b ? a : b; }
char buf[1 << 23], *p_1 = buf, *p_2 = buf, obuf[1 << 23], *O_ = obuf;
struct Fread {
} fin, fout;
int f_;
char ch_, Endl = '\n';
inline Fread& operator>>(Fread& F, int& a) {
  f_ = 1;
  a = 0;
  ch_ = getchar();
  while (ch_ < '0' || ch_ > '9') {
    if (ch_ == '-') f_ = -1;
    ch_ = getchar();
  }
  while (ch_ >= '0' && ch_ <= '9') {
    a = (a << 3) + (a << 1) + (ch_ ^ 48);
    ch_ = getchar();
  }
  a *= f_;
  return F;
}
Fread& operator<<(Fread& F, const int& a) {
  if (a < 0) {
    *O_++ = '-';
    return F << -a;
  }
  if (a > 9) F << (a / 10);
  *O_++ = a % 10 + '0';
  return F;
}
Fread& operator<<(Fread& F, const char& c) {
  *O_++ = c;
  return F;
}
const int maxn = 2e5 + 1e2;
const int mod = 998244353;
int n, m, a[maxn], b[maxn], pnt[maxn], vis[maxn];
int main() {
  int T;
  fin >> T;
  while (T--) {
    fin >> n >> m;
    int mn = 1e9 + 1e2, mx = -1e9 - 1e2, ans = 1;
    for (int i = 1; i <= n; i++) fin >> a[i];
    for (int i = 1; i <= m; i++) fin >> b[i];
    for (int i = 1; i <= n; i++) pnt[a[i]] = i;
    for (int i = 1; i <= n; i++) vis[i] = 0;
    for (int i = 1; i <= m; i++) vis[pnt[b[i]]] = true;
    vis[0] = vis[n + 1] = true;
    for (int i = 1; i <= m; i++) {
      (ans *= 2 - vis[pnt[b[i]] - 1] - vis[pnt[b[i]] + 1]) %= mod;
      vis[pnt[b[i]]] = false;
    }
    fout << ans << Endl;
  }
  fwrite(obuf, O_ - obuf, 1, stdout);
  return 0;
}
