## ProLights的博客

Someday, you can program the lights.

# Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) C. Felicity is Coming!组合学+集合

C. Felicity is Coming!
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

It's that time of the year, Felicity is around the corner and you can see people celebrating all around the Himalayan region. The Himalayan region has n gyms. The i-th gym has gi Pokemon in it. There are m distinct Pokemon types in the Himalayan region numbered from 1 to m. There is a special evolution camp set up in the fest which claims to evolve any Pokemon. The type of a Pokemon could change after evolving, subject to the constraint that if two Pokemon have the same type before evolving, they will have the same type after evolving. Also, if two Pokemon have different types before evolving, they will have different types after evolving. It is also possible that a Pokemon has the same type before and after evolving.

Formally, an evolution plan is a permutation f of {1, 2, ..., m}, such that f(x) = y means that a Pokemon of type x evolves into a Pokemon of type y.

The gym leaders are intrigued by the special evolution camp and all of them plan to evolve their Pokemons. The protocol of the mountain states that in each gym, for every type of Pokemon, the number of Pokemon of that type before evolving any Pokemon should be equal the number of Pokemon of that type after evolving all the Pokemons according to the evolution plan. They now want to find out how many distinct evolution plans exist which satisfy the protocol.

Two evolution plans f1 and f2 are distinct, if they have at least one Pokemon type evolving into a different Pokemon type in the two plans, i. e. there exists an i such that f1(i) ≠ f2(i).

Your task is to find how many distinct evolution plans are possible such that if all Pokemon in all the gyms are evolved, the number of Pokemon of each type in each of the gyms remains the same. As the answer can be large, output it modulo 109 + 7.

Input

The first line contains two integers n and m (1 ≤ n ≤ 1051 ≤ m ≤ 106) — the number of gyms and the number of Pokemon types.

The next n lines contain the description of Pokemons in the gyms. The i-th of these lines begins with the integer gi (1 ≤ gi ≤ 105) — the number of Pokemon in the i-th gym. After that gi integers follow, denoting types of the Pokemons in the i-th gym. Each of these integers is between 1 and m.

The total number of Pokemons (the sum of all gi) does not exceed 5·105.

Output

Output the number of valid evolution plans modulo 109 + 7.

Examples
input
2 3
2 1 2
2 2 3

output
1

input
1 3
3 1 2 3

output
6

input
2 4
2 1 2
3 2 3 4

output
2

input
2 2
3 2 2 1
2 1 2

output
1

input
3 7
2 1 2
2 3 4
3 5 6 7

output
24

Note

In the first case, the only possible evolution plan is:

In the second case, any permutation of (1,  2,  3) is valid.

In the third case, there are two possible plans:

In the fourth case, the only possible evolution plan is:

Source

My Solution

//这题是根据Codeforces的官方题解写的

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 1e6 + 8;
const LL MOD = 1e9 + 7;

LL factor[maxn];
inline LL mod(const LL x)
{
return x - x / MOD * MOD;
}
int main()
{
#ifdef LOCAL
freopen("c.txt", "r", stdin);
//freopen("c.out", "w", stdout);
int T = 5;
while(T--){
#endif // LOCAL
ios::sync_with_stdio(false); cin.tie(0);

factor[0] = factor[1] = 1;
for(int i = 2; i < maxn; i++){
factor[i] = mod(factor[i - 1] * i);
}
int n, m, gi, x;
cin >> n >> m;
vector<vector<int>> vec(m);

int i, j;
for(i = 0; i < n; i++){
cin >> gi;
for(j = 0; j < gi; j++){
cin >> x;
vec[x-1].push_back(i);
}
}

for(i = 0; i < m; i++){
sort(vec[i].begin(), vec[i].end());
}
sort(vec.begin(), vec.end());//化O(n^2) 为O(nlogn)

LL ans = 1, cnt = 1;
for(i = 1; i < m; i++){
if(vec[i] == vec[i - 1]){
cnt++;
}
else{
ans = mod(ans * factor[cnt]);
cnt  = 1;
}
}
ans = mod(ans * factor[cnt]);

cout << ans << endl;

#ifdef LOCAL
cout << endl;
}
#endif // LOCAL
return 0;
}



Thank you!

------from ProLights

#### codeforces 2-3-numbers

2018-03-18 09:26:26

#### 2-3-numbers(Codeforces)

2018-03-28 20:40:51

#### Codeforces 263 C. Circle of Numbers D. Cycle in Graph (dfs一般难度)

2014-09-09 22:07:18

#### codeforces 863C 1-2-3

2017-09-23 13:41:14

#### Codeforces Round #484 (Div. 2)

2018-05-18 17:40:51

#### CodeForces 932F Escape Through Leaf [set启发式合并+维护凸包+二分]

2018-02-23 14:23:50

#### CodeForces ~ 987D ~ Fair （思维 + 多源BFS）

2018-05-31 19:29:54

#### 1 + 2 + 3 + 4 + ⋯

2016-07-27 11:53:37

#### 【打CF，学算法——三星级】CodeForces 645C Enduring Exodus （二分+贪心）

2016-07-14 09:48:30

#### Codeforces 932E Team Work 组合数学+动态规划

2018-02-22 16:49:52