#include <bits/stdc++.h>
using namespace std;
const int N = 2e5, M = 998244353;
int a[N], b[N], p[N], tc, n, vis[N], k;
inline void mul(long long &a, long long b) {
  a = (a * b) % M;
  return;
}
int main() {
  cin >> tc;
  while (tc--) {
    cin >> n >> k;
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
      --a[i];
      p[a[i]] = i;
      vis[i] = 0;
    }
    for (int i = 0; i < k; ++i) {
      cin >> b[i];
      --b[i];
      vis[p[b[i]]] = 1;
    }
    long long ans = 1;
    for (int i = 0; i < k; ++i) {
      int x = 0;
      if (p[b[i]] > 0) {
        x += !vis[p[b[i]] - 1];
      }
      if (p[b[i]] < n - 1) {
        x += !vis[p[b[i]] + 1];
      }
      vis[p[b[i]]] = 0;
      mul(ans, x);
    }
    cout << ans << '\n';
  }
}
