#include <bits/stdc++.h>
using namespace std;
long long int t, n, k, a[200005], b[200005], ans, cnt;
set<long long int> s;
map<long long int, long long int> m;
int main() {
  cin >> t;
  while (t--) {
    ans = 1;
    s.clear();
    m.clear();
    cin >> n >> k;
    for (long long i = 0; i < n; i++) cin >> a[i];
    for (long long i = 0; i < k; i++) cin >> b[i];
    for (long long i = 0; i < n; i++) m[a[i]] = i;
    for (int i = k - 1; i >= 0; i--) {
      cnt = 0;
      if (m[b[i]] + 1 >= n or s.count(a[m[b[i]] + 1])) cnt++;
      if (m[b[i]] - 1 < 0 or s.count(a[m[b[i]] - 1])) cnt++;
      if (cnt == 2) {
        cout << 0 << endl;
        goto end;
      }
      if (cnt == 0) ans *= 2;
      ans %= 998244353;
      ;
      s.insert(b[i]);
    }
    cout << ans << endl;
  end:;
  }
  return 0;
}
