#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    int a[n], b[k];
    unordered_map<int, int> m1, m2;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      m1[a[i]] = i;
    }
    for (int i = 0; i < k; i++) {
      cin >> b[i];
      m2[b[i]] = i;
    }
    long long ans = 1;
    long long mod = 998244353;
    for (int i = 0; i < k; i++) {
      int idx = m1[b[i]];
      int ways = 0;
      if (idx - 1 >= 0 && !m2.count(a[idx - 1])) ways++;
      if (idx + 1 < n && !m2.count(a[idx + 1])) ways++;
      ans = (ans * ways) % mod;
      m2.erase(b[i]);
    }
    cout << ans << endl;
  }
}
