#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 2e5 + 10;
const long long INF = 2e9;
const long long mod = 998244353;
long long T, n, m, k, ans = 1;
long long a[MAXN], b[MAXN];
long long buk[MAXN];
signed main() {
  cin >> T;
  while (T--) {
    cin >> n >> k;
    for (long long i = 1; i <= n; ++i) cin >> a[i], buk[i] = 0;
    for (long long i = 1; i <= k; ++i) {
      cin >> b[i];
      buk[b[i]] = i;
    }
    long long cnt = 0;
    ans = 1;
    a[0] = a[n + 1] = n + 1;
    buk[n + 1] = INF;
    for (long long i = 1; i <= n; ++i) {
      if (!buk[a[i]] || !buk[a[i - 1]] || !buk[a[i + 1]]) continue;
      if (buk[a[i - 1]] > buk[a[i]] && buk[a[i + 1]] > buk[a[i]]) {
        ans = 0;
        break;
      }
    }
    if (!ans) {
      cout << ans << endl;
      continue;
    }
    buk[a[0]] = 0;
    for (long long i = 1; i <= n; ++i) {
      if (!buk[a[i - 1]] && buk[a[i]]) cnt++;
    }
    if (buk[a[1]]) cnt--;
    if (buk[a[n]]) cnt--;
    for (long long i = 1; i <= cnt; ++i) {
      (ans *= 2) %= mod;
    }
    cout << ans << endl;
  }
  return 0;
}
