#include <bits/stdc++.h>
using namespace std;
long long t, n, m, k, ccnt, pos = 0, sum = 0, minn2 = INT_MAX, sum2, cnt = 0,
                            cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0, cnt5 = 0,
                            cnt6 = 0, cnt7 = 0, a[1000005], x1, y7, x2, y2, x3,
                            y3, aaa, bbb;
long long vis[1000005], b[1000005], c[1000005], c3[105][105], c4[1000005],
    kk[1000005], x, y, z, d = 0, minn = LONG_LONG_MAX, maxx = -LONG_LONG_MAX;
long long pos1 = 0, pos2 = 0, tem, tem2, l, r, ans = 0, ans2, mod = 998244353,
          tw[100005], th[100005], dd, ee, out[1000005], dp[205][407],
          te[100005];
string s1, s2, s;
char cc[1005][1005], ch, cr[100005], ch2;
double fir[100005], sec[100005], tim, speed;
map<string, long long> m1;
long long fpow(long long aa, long long bb) {
  long long kk1 = aa;
  long long kk2 = 1;
  while (bb > 1) {
    if (bb % 2) {
      kk2 *= kk1;
      kk2 %= mod;
    }
    kk1 *= kk1;
    kk1 %= mod;
  }
  return (kk1 * kk2) % mod;
}
int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  cin >> t;
  while (t--) {
    cin >> n >> m;
    for (int i = 0; i <= n; i++) {
      a[i] = 0;
      b[i] = 0;
      c[i] = 0;
      vis[i] = 0;
    }
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      c[a[i]] = i;
    }
    for (int i = 0; i < m; i++) {
      cin >> b[i];
      vis[c[b[i]]] = -1;
    }
    ans = 1;
    for (int i = 0; i < m; i++) {
      cnt = 0;
      if (c[b[i]] > 0) {
        if (vis[c[b[i]] - 1] != -1) {
          cnt++;
        }
      }
      if (c[b[i]] < n - 1) {
        if (vis[c[b[i]] + 1] != -1) {
          cnt++;
        }
      }
      ans = ans * cnt;
      ans = ans % mod;
      vis[c[b[i]]] = 0;
    }
    cout << ans << '\n';
  }
}
