#include <bits/stdc++.h>
using namespace std;
const long long INF = 2147483647;
const long long MOD = 1000000007;
const long long mod = 998244353;
const long double eps = 1e-12;
long long n, k;
long long arr[200010];
long long pos[200010];
long long target[200010];
long long rk[200010];
long long res;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long T;
  cin >> T;
  while (T--) {
    cin >> n >> k;
    for (long long i = 1; i <= n; i++) {
      cin >> arr[i];
      pos[arr[i]] = i;
      rk[i] = 0;
    }
    for (long long i = 1; i <= k; i++) {
      cin >> target[i];
      rk[pos[target[i]]] = i;
    }
    rk[0] = INF;
    rk[n + 1] = INF;
    res = 1;
    for (long long i = 1; i <= k; i++) {
      long long cnt = 0;
      if (rk[pos[target[i]] - 1] < i) {
        cnt++;
      }
      if (rk[pos[target[i]] + 1] < i) {
        cnt++;
      }
      res *= cnt;
      res %= mod;
    }
    cout << res << endl;
  }
  return 0;
}
