#include <bits/stdc++.h>   using namespace std;   void display(vector<int> &a) {     for (int z : a) cout << z <<    ;     cout << endl;   }   int dx[4] = {0, 0, 1, -1};   int dy[4] = {1, -1, 0, 0};   const int mod = 998244353;   const long long INF64 = 3e18;   void smxl(long long &a, long long b) {     if (a < b) a = b;   }   void smnl(long long &a, long long b) {     if (a > b) a = b;   }   void adsl(long long &a, long long b) {     a += b;     if (a >= mod) a -= mod;   }   void misl(long long &a, long long b) {     a -= b;     if (a >= mod) a -= mod;     if (a < 0) a += mod;   }   void smx(long long &a, long long b) {     if (a < b) a = b;   }   void smn(long long &a, long long b) {     if (a > b) a = b;   }   void ads(long long &a, long long b) {     a += b;     if (a >= mod) a -= mod;   }   void mis(long long &a, long long b) {     a -= b;     if (a >= mod) a -= mod;     if (a < 0) a += mod;   }   long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); }   long long egcd(long long a, long long b, long long &x, long long &y) {     if (a == 0) {       x = 0;       y = 1;       return b;     }     long long x1, y1;     long long d = egcd(b % a, a, x1, y1);     x = y1 - (b / a) * x1;     y = x1;     return d;   }   long long mbinp(long long a, long long b) {     a %= mod;     if (b == 0) return 1;     long long ans = mbinp(a, b / 2);     long long tmp = (ans * ans) % mod;     if (b % 2) return ((tmp * a) % mod);     return ((tmp) % mod);   }   long long binp(long long a, long long b) {     if (b == 0) return 1;     long long ans = binp(a, b / 2);     long long tmp = (ans * ans);     if (b % 2) return ((tmp * a));     return ((tmp));   }   long long C(int n, int m) {     long long ret = 1;     for (int i = 1; i <= m; i++) {       ret *= (n - i + 1);       ret /= i;     }     return ret;   }   long long overbinp(long long a, int b) {     long long res = 1;     while (b) {       if (b & 1) {         if (res < INF64 / a)           res *= a;         else           return INF64;       }       if (b > 1) {         if (a < INF64 / a)           a *= a;         else           return INF64;       }       b >>= 1;     }     return res;   }   class DSU {     vector<int> par;     vector<int> siize;       public:     DSU(int n) {       par.resize(n);       siize.resize(n);       for (int i = 0; i < n; i++) {         par[i] = i;         siize[i] = 1;       }     }       public:     int get(int x) { return (x == par[x] ? x : par[x] = get(par[x])); }       public:     void merge(int a, int b) {       int x = get(a);       int y = get(b);       if (x == y) return;       if (siize[x] < siize[y]) swap(x, y);       par[y] = x;       siize[x] += siize[y];     }   };   class BinaryLift {     vector<vector<int> > binlift;     int n;       public:     BinaryLift(vector<int> rnk, vector<int> par) {       n = (int)par.size();       binlift.resize(n);       for (int i = 0; i < n; i++) binlift[i].resize(20);       for (int i = 0; i < n; i++) binlift[i][0] = par[i];       for (int j = 1; j < 20; j++)         for (int i = 0; i < n; i++) {           if ((1 << j) < rnk[i])             binlift[i][j] = binlift[binlift[i][j - 1]][j - 1];           else             binlift[i][j] = -1;         }     }       public:     int get_kth_ancestor(int x, int k) {       int pt = x;       for (int i = 19; i >= 0; i--) {         if (pt == -1) exit(0);         if (k & (1 << i)) pt = binlift[pt][i];       }       return pt;     }       public:     int get_th_ancestor(int x, int k) {       int pt = x;       for (int i = 19; i >= 0; i--) {         if (k & (1 << i)) pt = binlift[pt][i];       }       return pt;     }   };   class SparseTable2D {     vector<vector<vector<vector<int> > > > sparse;     vector<vector<int> > inp;     int m, n;       private:     int lg2(int x) {       int out = 0;       while ((1 << out) <= x) out++;       return out - 1;     }       public:     int rmin(int x1, int y1, int x2, int y2) {       int lenx = x2 - x1 + 1;       int lx = lg2(lenx) + 1;       int leny = y2 - y1 + 1;       int ly = lg2(leny) + 1;       return min(           min(sparse[lx][x1][ly][y1], sparse[lx][x1][ly][y2 + 1 - (1 << ly)]),           min(sparse[lx][x2 + 1 - (1 << lx)][ly][y1],               sparse[lx][x2 + 1 - (1 << lx)][ly][y2 + 1 - (1 << ly)]));     }       public:     SparseTable2D(vector<vector<int> > input, string param) {       n = input.size();       m = input[0].size();       inp = input;       if (param ==  min ) prepromin();     }       private:     void prepromin() {       int lln, lm;       lln = lg2(n) + 1;       lm = lg2(m) + 1;       sparse.resize(lln);       for (int i = 0; i < lln; i++) sparse[i].resize(n);       for (int i = 0; i < lln; i++)         for (int j = 0; j < n; j++) sparse[i][j].resize(lm);       for (int i = 0; i < lln; i++)         for (int j = 0; j < n; j++)           for (int k = 0; k < lm; k++) sparse[i][j][k].resize(m);       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) sparse[0][i][0][j] = inp[i][j];         for (int j = 1; j < lm; j++)           for (int k = 0; k + (1 << j) - 1 < m; k++)             sparse[0][i][j][k] = min(sparse[0][i][j - 1][k],                                      sparse[0][i][j - 1][k + (1 << (j - 1))]);       }       for (int i = 1; i < lln; i++)         for (int j = 0; j + (1 << i) - 1 < n; j++)           for (int k = 0; k < lm; k++)             for (int h = 0; h < m; h++)               sparse[i][j][k][h] = min(sparse[i - 1][j][k][h],                                        sparse[i - 1][j + (1 << (i - 1))][k][h]);     }   };   class SparseTable {     vector<vector<long long> > sparse;     int n;     vector<int> input;       private:     int lg2(int x) {       int out = 0;       while ((1 << out) <= x) out++;       return out - 1;     }       public:     int rmaxpos(int left, int right) {       int len = right - left + 1;       int lg = lg2(len);       return (input[sparse[left][lg]] > input[sparse[left + len - (1 << lg)][lg]]                   ? sparse[left][lg]                   : sparse[left + len - (1 << lg)][lg]);     }       public:     int rmaxval(int left, int right) {       int len = right - left + 1;       int lg = lg2(len);       return (input[sparse[left][lg]] > input[sparse[left + len - (1 << lg)][lg]]                   ? input[sparse[left][lg]]                   : input[sparse[left + len - (1 << lg)][lg]]);     }       public:     int rminpos(int left, int right) {       int len = right - left + 1;       int lg = lg2(len);       return (input[sparse[left][lg]] < input[sparse[left + len - (1 << lg)][lg]]                   ? sparse[left][lg]                   : sparse[left + len - (1 << lg)][lg]);     }       public:     int rminval(int left, int right) {       int len = right - left + 1;       int lg = lg2(len);       return (input[sparse[left][lg]] < input[sparse[left + len - (1 << lg)][lg]]                   ? input[sparse[left][lg]]                   : input[sparse[left + len - (1 << lg)][lg]]);     }       public:     long long rsum(int left, int right) {       long long ans = 0;       int pos;       while (left <= right) {         for (int i = 19; i >= 0; i--)           if ((1 << i) <= right - left + 1) {             pos = i;             break;           }         ans += sparse[left][pos];         left = left + (1 << pos);       }       return ans;     }       public:     SparseTable(vector<int> inp, string operation) {       input = inp;       n = inp.size();       if (operation ==  min )         prepromin();       else if (operation ==  max )         prepromax();       else if (operation ==  sum )         preprosum();     }       private:     void prepromin() {       sparse.resize(n);       int x = lg2(n) + 1;       for (int i = 0; i < n; i++) sparse[i].resize(x);       for (int i = 0; i < n; i++) sparse[i][0] = i;       for (int j = 1; j < x; j++)         for (int i = 0; i + (1 << (j)) - 1 < n; i++)           sparse[i][j] =               (input[sparse[i][j - 1]] < input[sparse[i + (1 << (j - 1))][j - 1]]                    ? sparse[i][j - 1]                    : sparse[i + (1 << (j - 1))][j - 1]);     }     void prepromax() {       sparse.resize(n);       int x = lg2(n) + 1;       for (int i = 0; i < n; i++) sparse[i].resize(x);       for (int i = 0; i < n; i++) sparse[i][0] = i;       for (int j = 1; j < x; j++)         for (int i = 0; i + (1 << (j)) - 1 < n; i++)           sparse[i][j] =               (input[sparse[i][j - 1]] > input[sparse[i + (1 << (j - 1))][j - 1]]                    ? sparse[i][j - 1]                    : sparse[i + (1 << (j - 1))][j - 1]);     }     void preprosum() {       sparse.resize(n);       int x = lg2(n) + 1;       for (int i = 0; i < n; i++) sparse[i].resize(x);       for (int i = 0; i < n; i++) sparse[i][0] = input[i];       for (int j = 1; j < x; j++)         for (int i = 0; i + (1 << (j)) - 1 < n; i++)           sparse[i][j] = sparse[i][j - 1] + sparse[i + (1 << (j - 1))][j - 1];     }   };   class Vector {    public:     pair<long long, long long> x;       public:     Vector(pair<long long, long long> a, pair<long long, long long> b) {       x.first = b.first - a.first;       x.second = b.second - a.second;     }       public:     long double getMagnitude() {       return sqrtl(x.first * x.first + x.second * x.second);     }   };   class Line {    public:     pair<long long, long long> x, y;       public:     Line(pair<long long, long long> a, pair<long long, long long> b) {       x = a;       y = b;     }       private:     long double dotProduct(Vector a, Vector b) {       return a.x.first * b.x.first + a.x.second * b.x.second;     }       private:     long double crossProduct(Vector a, Vector b) {       return a.x.first * b.x.second - a.x.second * b.x.first;     }       private:     long double magnitude(Vector a) { return a.getMagnitude(); }       public:     long double distanceToA(pair<long long, long long> c) {       return dotProduct(Vector(x, y), Vector(x, c)) / magnitude(Vector(x, y));     }       public:     long double orthogonalDistance(pair<long long, long long> c) {       return crossProduct(Vector(x, y), Vector(x, c)) / magnitude(Vector(x, y));     }       public:     pair<long double, long double> intersection(Line l) {       pair<long double, long double> ans;       ans.first = (long double)((x.first * y.second - x.second * y.first) *                                     (l.x.first - l.y.first) -                                 (x.first - y.first) * (l.x.first * l.y.second -                                                        l.x.second * l.y.first)) /                   ((x.first - y.first) * (l.x.second - l.y.second) -                    (x.second - y.second) * (l.x.first - l.y.first));       ans.second =           (long double)((x.first * y.second - x.second * y.first) *                             (l.x.second - l.y.second) -                         (x.second - y.second) *                             (l.x.first * l.y.second - l.x.second * l.y.first)) /           ((x.first - y.first) * (l.x.second - l.y.second) -            (x.second - y.second) * (l.x.first - l.y.first));       return ans;     }   };   class PruferCode {     vector<int> code;     vector<pair<int, int> > edges;       public:     PruferCode(vector<int> cc) {       code = cc;       findEdges();     }       private:     void findEdges() {       map<int, int> mp;       set<int> has;       set<int> wait;       for (int z : code) {         mp[z]++;         has.insert(z);       }       for (int i = 0; i < code.size() + 2; i++)         if (!has.count(i)) wait.insert(i);       for (int i = 0; i < code.size(); i++) {         int now = *wait.begin();         edges.push_back(make_pair(now, code[i]));         mp[now]++;         mp[code[i]]--;         if (mp[code[i]] == 0) {           has.erase(code[i]);           wait.insert(code[i]);         }         wait.erase(now);       }       assert(wait.size() == 2);       edges.push_back(make_pair(*wait.begin(), *wait.rbegin()));     }       public:     vector<pair<int, int> > getEdges() { return edges; }   };   class Segment {     pair<long long, long long> x, y;       public:     Segment(pair<long long, long long> a, pair<long long, long long> b) {       x = a;       y = b;     }       private:     long double dotProduct(Vector a, Vector b) {       return a.x.first * b.x.first + a.x.second * b.x.second;     }       private:     long double crossProduct(Vector a, Vector b) {       return a.x.first * b.x.second - a.x.second * b.x.first;     }       private:     long double magnitude(Vector a) { return a.getMagnitude(); }       public:     long double distanceToA(pair<long long, long long> c) {       return dotProduct(Vector(x, y), Vector(x, c)) / magnitude(Vector(x, y));     }       public:     long double distanceToSegment(pair<long long, long long> c) {       if (distanceToA(c) >= 0 && distanceToA(c) <= magnitude(Vector(x, y)))         return crossProduct(Vector(x, y), Vector(x, c)) / magnitude(Vector(x, y));       else         return min(magnitude(Vector(x, c)), magnitude(Vector(y, c)));     }   };   class HopcroftKarp {     vector<int> matched;     vector<vector<pair<int, int> > > adj;     int left;     int right;       public:     HopcroftKarp(vector<vector<pair<int, int> > > inp, int l, int r) {       adj = inp;       left = l;       matched.resize(l);       for (int i = 0; i < l; i++) matched[i] = -1;       right = r;     }       public:     vector<int> match() {       bool cont = true;       set<int> lfree, rfree;       for (int i = 0; i < left; i++) lfree.insert(i);       for (int i = left; i < left + right; i++) rfree.insert(i);       vector<bool> yet(left, 0);       for (int i = 0; i < left; i++)         for (int j = 0; j < adj[i].size(); j++)           if (adj[i][j].second == 1 && rfree.count(adj[i][j].first) && !yet[i]) {             yet[i] = true;             matched[i] = adj[i][j].first;             adj[i][j].second = 2;             for (int x = 0; x < adj[adj[i][j].first].size(); x++)               if (adj[adj[i][j].first][x].first == i)                 adj[adj[i][j].first][x].second = 2;             rfree.erase(adj[i][j].first);             lfree.erase(i);           }       while (cont) {         vector<int> par(left + right, -1);         queue<pair<int, int> > kyou;         for (int z : lfree) kyou.push(make_pair(z, 0));         int update = -1;         vector<int> vis(left + right, 0);         while (kyou.size()) {           pair<int, int> frt = kyou.front();           kyou.pop();           if (rfree.count(frt.first)) {             update = frt.first;             break;           }           if (frt.second == 0) {             for (pair<int, int> z : adj[frt.first]) {               if (z.second == 1 && !vis[z.first]) {                 par[z.first] = frt.first;                 vis[z.first] = 1;                 kyou.push(make_pair(z.first, 1));               }             }           } else {             for (pair<int, int> z : adj[frt.first]) {               if (z.second == 2 && !vis[z.first]) {                 par[z.first] = frt.first;                 vis[z.first] = 1;                 kyou.push(make_pair(z.first, 0));               }             }           }         }         int x = update;         int cnt = 0;         while (x != -1 && par[x] != -1) {           for (int i = 0; i < adj[x].size(); i++)             if (adj[x][i].first == par[x]) {               adj[x][i].second = (cnt == 0 ? 2 : 1);               if (cnt == 0) {                 matched[par[x]] = x;                 rfree.erase(x);                 lfree.erase(par[x]);               }             }           for (int i = 0; i < adj[par[x]].size(); i++)             if (adj[par[x]][i].first == x)               adj[par[x]][i].second = (cnt == 0 ? 2 : 1);           cnt++;           cnt %= 2;           x = par[x];         }         if (update == -1) cont = false;       }       return matched;     }   };   class Triangle {     pair<long long, long long> x, y, z;       public:     Triangle(pair<long long, long long> a, pair<long long, long long> b,              pair<long long, long long> c) {       x = a;       y = b;       z = c;     }       private:     long double crossProduct(Vector a, Vector b) {       return a.x.first * b.x.second - a.x.second * b.x.first;     }       public:     long double perimeter() {       long double s1, s2, s3;       s1 = Vector(x, y).getMagnitude();       s2 = Vector(y, z).getMagnitude();       s3 = Vector(x, z).getMagnitude();       return s1 + s2 + s3;     }       public:     long double area() { return abs(crossProduct(Vector(x, y), Vector(x, z))); }   };   class SuffixArray {     vector<int> order;     vector<int> lcp;     string str;       public:     SuffixArray(string in) {       str = in;       str +=  $ ;       order.resize(str.length());       lcp.resize(str.length());       compute();     }     void compute() {       vector<pair<char, int> > a;       vector<int> equi(order.size());       vector<int> bij(order.size());       for (int i = 0; i < str.length(); i++) a.push_back(make_pair(str[i], i));       sort(a.begin(), a.end());       for (int i = 0; i < str.length(); i++) order[i] = a[i].second;       equi[order[0]] = 0;       int r = 0;       for (int i = 1; i < str.length(); i++) {         if (a[i].first == a[i - 1].first)           equi[order[i]] = r;         else           equi[order[i]] = ++r;       }       int k = 0;       while ((1 << k) < str.length()) {         vector<pair<pair<int, int>, int> > a(str.length());         for (int i = 0; i < str.length(); i++)           a[i] = make_pair(               make_pair(equi[i], equi[(i + (1 << k)) % (str.length())]), i);         sort(a.begin(), a.end());         for (int i = 0; i < str.length(); i++) {           order[i] = a[i].second;           bij[order[i]] = i;         }         int r = 0;         equi[order[0]] = 0;         for (int i = 1; i < str.length(); i++) {           if (a[i].first == a[i - 1].first)             equi[order[i]] = r;           else             equi[order[i]] = ++r;         }         k++;       }       k = 0;       for (int i = 0; i < str.length() - 1; i++) {         int p = bij[i];         int j = order[p - 1];         while (i + k < str.length() && j + k < str.length() &&                str[i + k] == str[j + k])           k++;         lcp[p] = k;         k = max(k - 1, 0);       }     }       public:     int count(string ptr) {       int low = 0;       int hi = str.length() - 1;       int res1, res2;       res1 = 0;       res2 = 1e9;       while (low <= hi) {         int mid = (low + hi) / 2;         bool gr = false;         int i = 0;         for (i; i < min(ptr.length(), str.length() - order[mid]); i++) {           if (ptr[i] != str[order[mid] + i]) {             if (ptr[i] > str[order[mid] + i]) gr = true;             break;           }         }         if (i == ptr.length()) {           res2 = min(res2, mid);           hi = mid - 1;         } else if (!gr)           hi = mid - 1;         else           low = mid + 1;       }       low = 0;       hi = str.length() - 1;       while (low <= hi) {         int mid = (low + hi) / 2;         bool gr = false;         int i = 0;         for (i; i < min(ptr.length(), str.length() - order[mid] + 1); i++) {           if (ptr[i] != str[order[mid] + i]) {             if (ptr[i] > str[order[mid] + i]) gr = true;             break;           }         }         if (i == ptr.length()) {           res1 = max(res1, mid);           low = mid + 1;         } else if (!gr)           hi = mid - 1;         else           low = mid + 1;       }       if (res2 == 1e9) return 0;       return (res1 - res2 + 1);     }       public:     vector<int> get() { return order; }       public:     vector<int> getLcp() { return lcp; }       public:     long long diffSubstrings() {       long long out = 0;       for (int i = 1; i < str.length(); i++)         out += str.length() - order[i] - lcp[i] - 1;       return out;     }   };   string longestCommonSubstring(string a, string b) {     int len = 0;     string res = a +  %  + b;     SuffixArray sf = SuffixArray(res);     vector<int> order = sf.get();     vector<int> lcp = sf.getLcp();     vector<int> col(order.size());     for (int i = 0; i < order.size(); i++) {       if (order[i] < a.length())         col[order[i]] = 1;       else if (order[i] > a.length())         col[order[i]] = 2;     }     int pos = -1;     for (int i = 1; i < order.size(); i++)       if (col[order[i]] + col[order[i - 1]] == 3) {         if (lcp[i] > len) {           len = max(len, lcp[i]);           pos = (col[order[i]] == 1 ? order[i] : order[i - 1]);         }       }     return a.substr(pos, len);   }   void solve() {     int n, k;     cin >> n >> k;     vector<int> all(n);     vector<int> bij(n + 1);     set<int> st;     for (int i = 0; i < n; i++) {       cin >> all[i];       bij[all[i]] = i;       st.insert(i);     }     set<int> b;     vector<int> bb(k);     for (int i = 0; i < k; i++) {       int x;       cin >> x;       bb[i] = x;       b.insert(x);     }     long long ans = 1;     for (int i = 0; i < k; i++) {       int pos = bij[bb[i]];       auto t = st.find(pos);       auto m = t;       m++;       if (t == st.begin()) {         auto r = t;         r++;         if (b.count(all[*r])) {           cout << 0 <<   n ;           return;         } else {           st.erase(*r);         }       } else if (m == st.end()) {         auto r = t;         r--;         if (b.count(all[*r])) {           cout << 0 <<   n ;           return;         } else           st.erase(*r);       } else {         auto r = t;         auto q = t;         r++;         q--;         if (!b.count(all[*r]) && !b.count(all[*q])) {           ans *= 2;           ans %= mod;           st.erase(*r);         } else if (!b.count(all[*r]))           st.erase(*r);         else if (!b.count(all[*q]))           st.erase(*q);         else {           cout << 0 <<   n ;           return;         }       }       b.erase(bb[i]);     }     cout << ans <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int t;     cin >> t;     while (t--) solve();   }   