#include <bits/stdc++.h>
using namespace std;
const long long M = 998244353;
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long ans = 1;
    int n, k;
    cin >> n >> k;
    vector<int> a_idx(n + 1), b(n);
    vector<bool> b_yet(n + 1);
    set<pair<int, int>> a;
    for (int i = 0; i < n; i++) {
      int x;
      cin >> x;
      a.insert({i, x});
      a_idx[x] = i;
    }
    for (int i = 0; i < k; i++) {
      cin >> b[i];
      b_yet[b[i]] = 1;
    }
    for (int i = 0; i < k; i++) {
      auto itr = a.find({a_idx[b[i]], b[i]});
      if (itr == a.begin() && b_yet[next(itr)->second]) {
        ans = 0;
        break;
      }
      if (itr == prev(a.end()) && b_yet[prev(itr)->second]) {
        ans = 0;
        break;
      }
      if (b_yet[prev(itr)->second] && b_yet[next(itr)->second]) {
        ans = 0;
        break;
      }
      if (itr == a.begin() || b_yet[prev(itr)->second])
        a.erase(next(itr));
      else if (next(itr) == a.end() || b_yet[next(itr)->second])
        a.erase(prev(itr));
      else
        ans = ans * 2 % M;
      b_yet[b[i]] = 0;
    }
    cout << ans << endl;
  }
  return 0;
}
