#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
int a[200005];
int pos[200005];
int b[200005];
int vis[200005];
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n, k;
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; ++i) {
      scanf("%d", &a[i]);
      pos[a[i]] = i;
      vis[i] = 0;
    }
    for (int i = 1; i <= k; ++i) {
      scanf("%d", &b[i]);
      vis[pos[b[i]]] = 1;
    }
    long long res = 1;
    for (int i = 1; i <= k; ++i) {
      int p = pos[b[i]];
      int cnt = 0;
      if (p - 1 >= 1) {
        if (!vis[p - 1]) {
          ++cnt;
        }
      }
      if (p + 1 <= n) {
        if (!vis[p + 1]) {
          ++cnt;
        }
      }
      res = res * cnt % mod;
      vis[p] = 0;
    }
    printf("%lld\n", res);
  }
  return 0;
}
