#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int a[N], b[N], pos[N], cnt[N];
int n, m;
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%d%d", &n, &m);
    map<int, int> mp;
    for (int i = 0; i < n; i++) scanf("%d", &a[i]), pos[a[i]] = i;
    for (int i = 0; i < m; i++)
      scanf("%d", &b[i]), cnt[i] = pos[b[i]], mp[pos[b[i]]]++;
    long long ans = 1;
    for (int i = 0; i < m; i++) {
      if (cnt[i] == 0) {
        if (mp[1]) {
          ans = 0;
        }
        mp[0]--;
      } else if (cnt[i] == n - 1) {
        if (mp[n - 2]) {
          ans = 0;
        }
        mp[n - 1]--;
      } else {
        long long k = 2;
        if (mp[cnt[i] - 1]) k--;
        if (mp[cnt[i] + 1]) k--;
        ans *= k;
        ans %= 998244353;
        mp[cnt[i]]--;
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
