#include <bits/stdc++.h>
using namespace std;
const int inf = 1000000000;
long long mod = 1000000007LL;
long long mod2 = 998244353LL;
int t, n, k;
int a[200002];
int b[200002];
int p[200002];
int main() {
  cin >> t;
  for (int cas = 1; cas <= t; ++cas) {
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; ++i) {
      scanf("%d", &a[i]);
      p[a[i]] = i;
    }
    for (int i = 1; i <= k; ++i) {
      scanf("%d", &b[i]);
    }
    long long res = 1LL;
    unordered_set<int> ss;
    ss.clear();
    for (int i = k; i >= 1; --i) {
      int pos = p[b[i]];
      int cnt = 2;
      if (pos - 1 < 1 || ss.find(a[pos - 1]) != ss.end()) {
        cnt--;
      }
      if (pos + 1 > n || ss.find(a[pos + 1]) != ss.end()) {
        cnt--;
      }
      res = res * (long long)cnt % mod2;
      ss.insert(b[i]);
    }
    printf("%lld\n", res);
  }
  return 0;
}
