#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 5;
const int MOD = 998244353;
int arr[MAXN];
int rev[MAXN];
int l[MAXN];
int b[MAXN];
int r[MAXN];
bool exists[MAXN];
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
      cin >> arr[i];
      rev[arr[i]] = i;
    }
    for (int i = 1; i <= k; i++) {
      cin >> b[i];
      exists[b[i]] = true;
    }
    bool ok = true;
    exists[0] = true;
    arr[0] = 0;
    arr[n + 1] = 0;
    long long res = 1;
    for (int i = 1; i <= k; i++) {
      int cnt = 0;
      if (!exists[arr[rev[b[i]] + 1]]) {
        cnt++;
      }
      if (!exists[arr[rev[b[i]] - 1]]) {
        cnt++;
      }
      exists[b[i]] = false;
      res = 1LL * res * cnt;
      res %= MOD;
    }
    cout << res << endl;
  }
}
