#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10000;
const int MOD = 998244353;
int a[N], b[N];
int vis[N], st[N];
int t, n, k;
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%d %d", &n, &k);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]), vis[a[i]] = i;
    for (int i = 1; i <= k; i++) scanf("%d", &b[i]), st[b[i]] = i;
    long long ans = 1;
    for (int i = 1; i <= k; i++) {
      long long cnt = 0;
      if (vis[b[i]] != n && st[a[vis[b[i]] + 1]] == 0) cnt++;
      if (vis[b[i]] != 1 && st[a[vis[b[i]] - 1]] == 0) cnt++;
      ans = ans * cnt % MOD;
      st[b[i]] = 0;
    }
    printf("%lld\n", ans % MOD);
    for (int i = 1; i <= n; i++) vis[i] = st[i] = 0;
  }
}
