#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
long long int Lpow(long long int a, long long int b) {
  long long int ans = 1;
  while (b > 0) {
    if (b % 2) ans = (ans * a);
    a = (a * a);
    b = b / 2;
  }
  return ans;
}
long long int Lpow(long long int a, long long int b, long long int M) {
  long long int ans = 1;
  while (b > 0) {
    if (b % 2) ans = (ans * a) % M;
    a = (a * a) % M;
    b = b / 2;
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    vector<int> b(n + 1, 0), o(m), o1(n + 1, 0);
    set<pair<int, int>> a;
    for (int i = 0; i < n; i++) {
      int x;
      cin >> x;
      o1[x] = i;
      a.insert({i, x});
    }
    for (int i = 0; i < m; i++) {
      cin >> o[i];
      b[o[i]]++;
    }
    long long int ans = 1;
    for (int i = 0; i < m; i++) {
      if (a.find({o1[o[i]], o[i]}) == a.end()) {
        ans = 0;
        break;
      } else {
        auto x = a.find({o1[o[i]], o[i]});
        int tmp = 0;
        ++x;
        if (x != a.end()) {
          int l = (x)->second;
          if (b[l] == 0) tmp++;
        }
        --x;
        if (x != a.begin()) {
          int l = (--x)->second;
          if (b[l] == 0) tmp++;
          ++x;
        }
        if (tmp == 0) {
          ans = 0;
          break;
        } else {
          ans = (ans * tmp) % 998244353;
          a.erase(x);
          b[o[i]] = 0;
        }
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
