#include <bits/stdc++.h>
using namespace std;
long long mod = 998244353;
int a[200005], b[200005];
bool vis[2000005];
int pre[200005], nxt[200005], pos[200005];
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    long long ans = 1;
    for (int i = 1; i <= n; i++) {
      vis[i] = 0;
      pre[i] = i - 1;
      nxt[i] = i + 1;
      scanf("%d", &a[i]);
      pos[a[i]] = i;
    }
    for (int i = 1; i <= m; i++) {
      cin >> b[i];
      vis[pos[b[i]]] = 1;
    }
    for (int i = 1; i <= m; i++) {
      int k = 0;
      int p = pos[b[i]];
      if (!vis[nxt[p]] && nxt[p] < n + 1) k++;
      if (!vis[pre[p]] && pre[p] > 0) k++;
      if (!vis[nxt[p]] && nxt[p] < n + 1) {
        nxt[p] = nxt[nxt[p]];
      } else if (k) {
        pre[p] = pre[pre[p]];
      }
      ans *= k;
      ans %= mod;
      vis[p] = 0;
      if (!ans) break;
    }
    cout << ans << endl;
  }
  return 0;
}
