#include <bits/stdc++.h>
using namespace std;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vc<vc<T>>;
template <class T>
void mkuni(vector<T>& v) {
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
}
long long rand_int(long long l, long long r) {
  static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
  return uniform_int_distribution<long long>(l, r)(gen);
}
template <class T>
void print(T x, int suc = 1) {
  cout << x;
  if (suc == 1)
    cout << '\n';
  else
    cout << ' ';
}
template <class T>
void print(const vector<T>& v, int suc = 1) {
  for (int i = 0; i < v.size(); i++)
    print(v[i], i == (int)(v.size()) - 1 ? suc : 2);
}
const int maxn = 5e5 + 7;
const int mod = 998244353;
int n, k;
int qmod(int a, int b) {
  long long res = 1;
  while (a) {
    if (a & 1) res = res * b % mod;
    b *= b;
    a >>= 1;
  }
  return res;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  while (t--) {
    int qq = 0;
    for (int i = 1; i <= 100; ++i) {
      qq = qq * qmod(i, i);
    }
    cin >> n >> k;
    vector<int> a(n + 1), pos(n + 1), b(k + 1), tk(n, 0);
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
      --a[i];
      pos[a[i]] = i;
    }
    for (int i = 0; i < k; ++i) {
      cin >> b[i];
      --b[i];
      tk[b[i]] = 1;
    }
    long long res = 1;
    for (int i = 0; i < k; ++i) {
      int p = pos[b[i]];
      int ret = 0;
      if (p + 1 < n && !tk[a[p + 1]]) ++ret;
      if (p - 1 > -1 && !tk[a[p - 1]]) ++ret;
      res = (1ll * res * ret) % mod;
      tk[b[i]] = 0;
    }
    cout << res << endl;
  }
  return 0;
}
