#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 998244353;
int main() {
  int t;
  cin >> t;
  vector<ll> ans;
  while (t--) {
    int n, k;
    cin >> n >> k;
    int a[n], b[k];
    for (int i = 0; i < (int)n; i++) {
      scanf("%d", &a[i]);
      a[i]--;
    }
    for (int i = 0; i < (int)k; i++) {
      scanf("%d", &b[i]);
      b[i]--;
    }
    int pos[n];
    for (int i = 0; i < (int)n; i++) pos[a[i]] = i;
    vector<bool> use(n, true);
    for (int i = 0; i < (int)k; i++) use[b[i]] = false;
    ll val = 1;
    for (int i = 0; i < (int)k; i++) {
      int id = pos[b[i]];
      ll res = 0;
      if (id > 0 and use[a[id - 1]]) res++;
      if (id < n - 1 and use[a[id + 1]]) res++;
      if (i == n - 2) res = 1;
      (val *= res) %= mod;
      use[b[i]] = true;
    }
    ans.push_back(val);
  }
  for (auto x : ans) cout << x << '\n';
  return 0;
}
