C. Felicity is Coming!

Codeforces
IDCF757C
Time2000ms
Memory256MB
Difficulty
data structureshashingsortingsstrings
English · Original
Chinese · Translation
Formal · Original
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 _g__i_ 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 _f_1 and _f_2 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 _f_1(_i_) ≠ _f_2(_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_ ≤ 105, 1 ≤ _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 _g__i_ (1 ≤ _g__i_ ≤ 105) — the number of Pokemon in the _i_\-th gym. After that _g__i_ 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 _g__i_) does not exceed 5·105. ## Output Output the number of valid evolution plans modulo 109 + 7. [samples] ## 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:
又到了一年中的这个时候,Felicity即将到来,你可以在喜马拉雅地区各地看到人们庆祝的景象。喜马拉雅地区有 #cf_span[n] 个道馆。第 #cf_span[i] 个道馆中有 #cf_span[gi] 只宝可梦。喜马拉雅地区共有 #cf_span[m] 种不同的宝可梦类型,编号从 #cf_span[1] 到 #cf_span[m]。节日中设立了一个特殊的进化营地,声称可以进化任何宝可梦。宝可梦的类型在进化后可能改变,但需满足以下约束:如果两只宝可梦在进化前类型相同,则进化后类型仍相同;如果两只宝可梦在进化前类型不同,则进化后类型仍不同。当然,也可能存在宝可梦进化前后类型不变的情况。 形式上,一个 _进化计划_ 是集合 #cf_span[{1, 2, ..., m}] 的一个排列 #cf_span[f],其中 #cf_span[f(x) = y] 表示类型为 #cf_span[x] 的宝可梦将进化为类型为 #cf_span[y] 的宝可梦。 道馆馆主们对这个特殊的进化营地感到着迷,他们都计划进化自己的宝可梦。山脉的协议规定:在每个道馆中,对于每种宝可梦类型,在进化任何宝可梦之前,该类型的宝可梦数量必须等于根据进化计划进化所有宝可梦后该类型的宝可梦数量。现在他们想要知道,有多少种不同的 _进化计划_ 满足这一协议。 两个进化计划 #cf_span[f1] 和 #cf_span[f2] 不同,当且仅当至少存在一种宝可梦类型在两个计划中进化为不同的类型,即存在某个 #cf_span[i] 使得 #cf_span[f1(i) ≠ f2(i)]。 你的任务是找出满足以下条件的不同的 _进化计划_ 的数量:当所有道馆中的所有宝可梦都按计划进化后,每个道馆中每种类型的宝可梦数量保持不变。由于答案可能很大,请输出对 #cf_span[109 + 7] 取模的结果。 第一行包含两个整数 #cf_span[n] 和 #cf_span[m] (#cf_span[1 ≤ n ≤ 105], #cf_span[1 ≤ m ≤ 106]) —— 道馆数量和宝可梦类型数量。 接下来的 #cf_span[n] 行描述了各道馆中的宝可梦。第 #cf_span[i] 行以整数 #cf_span[gi] (#cf_span[1 ≤ gi ≤ 105]) 开头,表示第 #cf_span[i] 个道馆中宝可梦的数量,随后是 #cf_span[gi] 个整数,表示该道馆中宝可梦的类型,每个整数介于 #cf_span[1] 和 #cf_span[m] 之间。 宝可梦总数(所有 #cf_span[gi] 的和)不超过 #cf_span[5·105]。 请输出满足条件的进化计划数量,对 #cf_span[109 + 7] 取模。 在第一个测试用例中,唯一的可能进化计划是: 在第二个测试用例中,#cf_span[(1,  2,  3)] 的任意排列都是有效的。 在第三个测试用例中,有两种可能的计划: 在第四个测试用例中,唯一的可能进化计划是: ## Input 第一行包含两个整数 #cf_span[n] 和 #cf_span[m] (#cf_span[1 ≤ n ≤ 105], #cf_span[1 ≤ m ≤ 106]) —— 道馆数量和宝可梦类型数量。接下来的 #cf_span[n] 行描述了各道馆中的宝可梦。第 #cf_span[i] 行以整数 #cf_span[gi] (#cf_span[1 ≤ gi ≤ 105]) 开头,表示第 #cf_span[i] 个道馆中宝可梦的数量,随后是 #cf_span[gi] 个整数,表示该道馆中宝可梦的类型,每个整数介于 #cf_span[1] 和 #cf_span[m] 之间。宝可梦总数(所有 #cf_span[gi] 的和)不超过 #cf_span[5·105]。 ## Output 请输出满足条件的进化计划数量,对 #cf_span[109 + 7] 取模。 [samples] ## Note 在第一个测试用例中,唯一的可能进化计划是:在第二个测试用例中,#cf_span[(1,  2,  3)] 的任意排列都是有效的。在第三个测试用例中,有两种可能的计划:在第四个测试用例中,唯一的可能进化计划是:
**Definitions** Let $ n, m \in \mathbb{Z}^+ $ denote the number of gyms and Pokemon types, respectively. Let $ G_i \subseteq \{1, 2, \dots, m\} $ be the multiset of Pokemon types in gym $ i $, for $ i \in \{1, \dots, n\} $. Let $ c_{i,t} \in \mathbb{Z}_{\geq 0} $ denote the count of Pokemon of type $ t $ in gym $ i $. Let $ \mathbf{c}_i = (c_{i,1}, c_{i,2}, \dots, c_{i,m}) \in \mathbb{Z}_{\geq 0}^m $ be the type-count vector for gym $ i $. An evolution plan is a permutation $ f: \{1, 2, \dots, m\} \to \{1, 2, \dots, m\} $. **Constraints** 1. $ 1 \leq n \leq 10^5 $, $ 1 \leq m \leq 10^6 $ 2. $ \sum_{i=1}^n |G_i| \leq 5 \cdot 10^5 $ 3. For each gym $ i $, the multiset of types after evolution under $ f $ must match the original multiset: $$ \forall i \in \{1, \dots, n\}, \quad \forall t \in \{1, \dots, m\}, \quad c_{i,t} = c_{i,f^{-1}(t)} $$ Equivalently, $ \mathbf{c}_i = \mathbf{c}_i \circ f^{-1} $, meaning the vector $ \mathbf{c}_i $ is invariant under permutation $ f $. **Objective** Count the number of permutations $ f \in S_m $ such that for all gyms $ i $, $ f $ preserves the type-count vector: $$ f(\text{supp}(\mathbf{c}_i)) \subseteq \text{supp}(\mathbf{c}_i) \quad \text{and} \quad \mathbf{c}_i \circ f = \mathbf{c}_i $$ Equivalently, $ f $ must map each type $ t $ to a type $ t' $ such that for all gyms $ i $, $ c_{i,t} = c_{i,t'} $. Define an equivalence relation on types: $$ t \sim t' \iff \forall i \in \{1, \dots, n\}, \; c_{i,t} = c_{i,t'} $$ Let $ \mathcal{P} = \{P_1, P_2, \dots, P_k\} $ be the partition of $ \{1, \dots, m\} $ into equivalence classes under $ \sim $. Then, the valid evolution plans are exactly the permutations that permute types within each equivalence class. Thus, the number of valid evolution plans is: $$ \prod_{j=1}^k |P_j|! $$ Compute this product modulo $ 10^9 + 7 $.
Samples
Input #1
2 3
2 1 2
2 2 3
Output #1
1
Input #2
1 3
3 1 2 3
Output #2
6
Input #3
2 4
2 1 2
3 2 3 4
Output #3
2
Input #4
2 2
3 2 2 1
2 1 2
Output #4
1
Input #5
3 7
2 1 2
2 3 4
3 5 6 7
Output #5
24
API Response (JSON)
{
  "problem": {
    "name": "C. Felicity is Coming!",
    "description": {
      "content": "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 _g__i_ Pokemon in i",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF757C"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "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 _g__i_ Pokemon in i...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "又到了一年中的这个时候,Felicity即将到来,你可以在喜马拉雅地区各地看到人们庆祝的景象。喜马拉雅地区有 #cf_span[n] 个道馆。第 #cf_span[i] 个道馆中有 #cf_span[gi] 只宝可梦。喜马拉雅地区共有 #cf_span[m] 种不同的宝可梦类型,编号从 #cf_span[1] 到 #cf_span[m]。节日中设立了一个特殊的进化营地,声称可以进化任何宝可梦。宝可...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n, m \\in \\mathbb{Z}^+ $ denote the number of gyms and Pokemon types, respectively.  \nLet $ G_i \\subseteq \\{1, 2, \\dots, m\\} $ be the multiset of Pokemon types in gym $ i $, for...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments