time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard 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 ≤ 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 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:

【题目链接】:http://codeforces.com/contest/757/problem/C

【题解】



记录最后每种动物在哪些gym里面出现过;

如果2动物在3号gym里面出现两次,在4号gym里面出现3次;

定义

    vector <int> a[MAXM];

则a[2]={3,3,4,4,4};

如果3号动物和2号之间能互相进化

则必有a[2]=a[3]

即2号动物在哪个gym里面出现过,3号动物也要在那个gym里面出现;

且要求出现的次数相同;(所以a[x]里面可能有重复的数字表示它在这个

gym里面出现多次);

(如果符合这样的要求,则2和3能够转化,且也这有这样才能满足转化过后2和3在各个gym里面出现的次数都不变);

然后用一个sort把a[1..m]都排序一下;

a里面的内容也要排序;

这样相同的a就会在靠在一起;

那些相同的a能够互相转化;

每个相同的a构成的若干个集合设为Si

则每个集合的方案数为(Si)!

答案就是

∏((Si)!)

预处理出1..MAXM的阶乘;

那些没有出现的动物;显然可以任意转化;

所以也是N!



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int MAXN = 110;
const int MAXM = 1e6+100;
const LL MOD = 1e9+7; int n,m;
vector <int> a[MAXM];
LL jc[MAXM]; int main()
{
//freopen("F:\\rush.txt","r",stdin);
jc[0] = 1;
for (LL i = 1;i <= 1000000;i++)
jc[i] = (jc[i-1]*i)%MOD;
rei(n);rei(m);
rep1(i,1,n)
{
int num;
rei(num);
rep1(j,1,num)
{
int x;
rei(x);
a[x].pb(i);
}
}
sort(a+1,a+1+m);
LL ans = 1;
rep1(i,1,m)
{
int j = i + 1;
while (j<=m && a[j]==a[i]) j++;
int len = j-i;
ans = (ans * jc[len])%MOD;
i = j-1;
}
cout << ans << endl;
return 0;
}

【codeforces 757C】Felicity is Coming!的更多相关文章

  1. 【codeforces 757D】Felicity's Big Secret Revealed

    [题目链接]:http://codeforces.com/problemset/problem/757/D [题意] 给你一个01串; 让你分割这个01串; 要求2切..n+1切; 对于每一种切法 所 ...

  2. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  3. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  4. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  5. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  6. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  7. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  8. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  9. 【Codeforces 670C】 Cinema

    [题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...

随机推荐

  1. 单元测试Assert类

    Assert类主要的静态成员 1. AreEqual:方法被重载了N多次,主要功能是判断两个值是否相等:如果两个值不相等,则测试失败. 2. AreNotEqual:方法被重载了N多次,主要功能是判断 ...

  2. robot framework 使用三:浏览器兼容性自己主动化

    robot framework 測试浏览器兼容性 上图中黄色圈的地方默认什么都不写.是firefox浏览器,写上ie就是ie浏览器了 firefox最新版本号即可.ie须要设置: 1. IE选项设置的 ...

  3. ZOJ 题目3587 Marlon&#39;s String(KMP)

    Marlon's String Time Limit: 2 Seconds      Memory Limit: 65536 KB Long long ago, there was a coder n ...

  4. 为什么golang的开发效率高(编译型的强类型语言、工程角度高、在开发上的高效率主要来自于后发优势,编译快、避免趁编译时间找产品妹妹搭讪,既是强类型语言又有gc,只要通过编译,非业务毛病就很少了)

    作者:阿猫链接:https://www.zhihu.com/question/21098952/answer/21813840来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出 ...

  5. MySQL系列之七:主从复制(转)

    一:实验环境 IP 操作系统 mysql版本号 master 192.168.25.11 CentOS7 5.6.35 slave 192.168.25.10 win10 5.7.18 slave版本 ...

  6. STL之vector容器的实现框架

    说明:本文仅供学习交流,转载请标明出处,欢迎转载. 实现vector容器的思路等同于实现一个动态数组,以下我们參照源代码的相关资料,给出一个vector容器的大致框架,仅仅有声明,没给出详细的实现. ...

  7. Javascript和jquery事件--事件监听器

    之前看完了js和jq的冒泡捕获和事件对象event,这里看看同时使用js和jq后我最容易混淆的监听器的绑定. (1) js的监听器绑定解绑 绑定监听器有两种方式: a.on-事件type,比如oncl ...

  8. javascript进阶课程--第三章--匿名函数和闭包

    javascript进阶课程--第三章--匿名函数和闭包 一.总结 二.学习要点 掌握匿名函数和闭包的应用 三.匿名函数和闭包 匿名函数 没有函数名字的函数 单独的匿名函数是无法运行和调用的 可以把匿 ...

  9. ThinkPHP视图查询

    ThinkPHP视图查询 一.总结 1.这里的视图查询和多表查询很像,当然多表查询的话肯定要支持左右链接查询 2.view:视图的使用,关键字是view 3.sql视图功能支持:thinkphp支持视 ...

  10. PL/SQL笔记(一)

    PL/SQL概述 PL/SQL是一种高级的数据库程序设计语言,专门使用与Oracle语言基于数据库的服务器的内部,所以PL/SQL代码可以对数据库进行快速的处理. 1.什么是PL/SQL? PL/SQ ...