#include <bits/stdc++.h>
using namespace std;
const long long infl = 1LL << 60;
int mod = 998244353;
int main() {
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    int a[n + 1], b[k + 1], inva[n + 1], invb[n + 1];
    memset(inva, -1, sizeof(inva));
    memset(invb, -1, sizeof(invb));
    a[0] = 0;
    b[0] = 0;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      inva[a[i]] = i;
    }
    for (int i = 1; i <= k; i++) {
      cin >> b[i];
      invb[b[i]] = i;
    }
    long long ans = 1;
    for (int i = 1; i <= k; i++) {
      int r = 0, l = 0;
      int x = inva[b[i]];
      if (x > 1 && invb[a[x - 1]] < i) l++;
      if (x < n && invb[a[x + 1]] < i) r++;
      ans *= (r + l);
      ans %= mod;
    }
    cout << ans << endl;
  }
  return 0;
}
