#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using db = double;
using vi = vector<int>;
const int inf = 0x3f3f3f3f;
const db eps = 1e-8;
const int mod = 998244353;
ll qpow(ll a, ll b) {
  ll ret = 1;
  while (b) {
    if (b & 1) ret = ret * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ret;
}
const int maxn = 2e5 + 7;
int a[maxn], b[maxn];
int vis[maxn], p[maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    int n, k;
    cin >> n >> k;
    for (int i = 0; i <= n + 1; i++) {
      p[i] = 0;
      vis[i] = 0;
    }
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      p[a[i]] = i;
    }
    a[n + 1] = 0;
    a[0] = 0;
    for (int i = 1; i <= k; i++) {
      cin >> b[i];
      vis[b[i]] = 1;
    }
    ll ans = 1;
    vis[0] = 1;
    for (int i = 1; i <= k; i++) {
      int tmp = b[i];
      int pos = p[tmp];
      if (vis[a[pos - 1]] == 1 && vis[a[pos + 1]] == 1) {
        ans = 0;
        break;
      } else {
        if (vis[a[pos - 1]] == 0 && vis[a[pos + 1]] == 0) {
          ans = ans * 2 % mod;
        }
      }
      vis[tmp] = 0;
    }
    cout << ans << '\n';
  }
}
