#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const long long N = 1000005;
struct Node {
  long long val;
  Node *prec = NULL;
  Node *succ = NULL;
} node[N];
void deleteNode(Node *p) {
  if (p == NULL) return;
  Node *prec = p->prec;
  Node *succ = p->succ;
  p->prec = p->succ = NULL;
  if (prec) prec->succ = succ;
  if (succ) succ->prec = prec;
}
void deleteNode(Node &p) { deleteNode(&p); }
long long n, m, a[N], b[N], f[N], c[N];
void clearall(long long n) {
  for (long long i = 0; i <= n + 1; i++)
    a[i] = b[i] = f[i] = c[i] = node[i].val = 0,
    node[i].prec = node[i].succ = 0;
}
void solve() {
  cin >> n >> m;
  clearall(n);
  for (long long i = 1; i <= n; i++) cin >> a[i];
  for (long long i = 1; i <= m; i++) cin >> b[i];
  for (long long i = 1; i <= m; i++) f[b[i]] = i;
  for (long long i = 1; i <= n; i++) node[i].val = a[i];
  for (long long i = 1; i <= n; i++) node[i].prec = node[i].succ = NULL;
  for (long long i = 1; i < n; i++) node[i].succ = &node[i + 1];
  for (long long i = 2; i <= n; i++) node[i].prec = &node[i - 1];
  for (long long i = 1; i <= n; i++) c[a[i]] = i;
  long long ans = 1;
  for (long long j = 1; j <= m; j++) {
    long long i = c[b[j]];
    Node *p = &node[i];
    Node *prec = p->prec;
    Node *succ = p->succ;
    if (prec && f[prec->val] > j) prec = NULL;
    if (succ && f[succ->val] > j) succ = NULL;
    if (prec && succ) {
      ans *= 2;
      ans %= mod;
      deleteNode(prec);
    } else if (prec) {
      deleteNode(prec);
    } else if (succ) {
      deleteNode(succ);
    } else {
      cout << 0 << endl;
      return;
    }
  }
  cout << ans << endl;
}
signed main() {
  ios::sync_with_stdio(false);
  long long t;
  cin >> t;
  while (t--) {
    solve();
  }
}
