#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e5 + 1;
const long long MOD = 998244353;
long long mul(long long a, long long b) { return (a * b) % MOD; }
struct node {
  int data;
  node* left = nullptr;
  node* right = nullptr;
};
void solve() {
  long long n, k;
  cin >> n >> k;
  vector<long long> a(n), b(k);
  vector<node> c(n);
  for (auto& i : a) {
    cin >> i;
  }
  unordered_map<int, node*> kek;
  unordered_map<int, bool> need;
  for (int i = 0; i < n; ++i) {
    c[i].data = a[i];
    kek[a[i]] = &c[i];
    if (i != n - 1) {
      c[i].right = &c[i + 1];
    }
    if (i != 0) {
      c[i].left = &c[i - 1];
    }
  }
  for (auto& i : b) {
    cin >> i;
    need[i] = true;
  }
  long long ans = 1;
  for (int i = 0; i < k; ++i) {
    node cur = *kek[b[i]];
    int cnt = 0;
    if (cur.left != nullptr && !need[cur.left->data]) {
      cnt++;
    }
    if (cur.right != nullptr && !need[cur.right->data]) {
      cnt++;
    }
    node* l = cur.left;
    node* r = cur.right;
    if (l != nullptr) l->right = r;
    if (r != nullptr) r->left = l;
    ans = mul(ans, cnt);
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
  long long test = 1;
  cin >> test;
  for (int t = 1; t <= test; ++t) {
    solve();
  }
  return 0;
}
