1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 6e5 + 5, P = 998244353, base = 19491001, P0 = 20090327, P1 = 20070923;
int T, n, m;
int fac[N], inv[N], cnt[N];
bool vis[N];
unsigned int a[N][2], b[N][2];
int su, en[N], lt[N], hd[N];
map<pair<unsigned int, unsigned int>, unsigned int> mp1, mp2;
map<pair<unsigned int, unsigned int>, bool> Vis;
void add(int u, int v) { en[++su] = v, lt[su] = hd[u], hd[u] = su; }
void init() {
fac[0] = 1;
for (int i = 1; i <= N - 5; i++) fac[i] = fac[i - 1] * i % P;
}
inline void solve() {
memset(a, 0, sizeof a);
memset(b, 0, sizeof b);
scanf("%lld%lld", &n, &m);
for (int i = 1; i <= m; i++) {
int x;
for (int j = 1; j <= n; j++) {
scanf("%lld", &x);
a[j][0] = (1ll * a[j][0] * base + x) % P0;
a[j][1] = (1ll * a[j][1] * base + x) % P1;
}
for (int j = 1; j <= n; j++) {
scanf("%lld", &x);
b[j][0] = (1ll * b[j][0] * base + x) % P0;
b[j][1] = (1ll * b[j][1] * base + x) % P1;
}
}
mp1.clear(), mp2.clear();
Vis.clear();
for (int i = 1; i <= n; i++) mp1[{ a[i][0], a[i][1] }]++, mp2[{ b[i][0], b[i][1] }]++;
int ans = 1;
for (int i = 1; i <= n; i++) {
if (!Vis[{ a[i][0], a[i][1] }]) {
Vis[{ a[i][0], a[i][1] }] = 1;
if (mp1[{ a[i][0], a[i][1] }] != mp2[{ a[i][0], a[i][1] }])
ans = 0;
else
ans = ans * fac[mp1[{ a[i][0], a[i][1] }]] % P;
}
}
printf("%lld\n", ans);
}
signed main() {
// freopen("interact.in","r",stdin);
// freopen("interact.out","w",stdout);
scanf("%lld", &T);
init();
while (T--) solve();
return 0;
}
|