#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000005;
const long long mod = 1e9 + 7;
const int N = 1e6;
vector<int> rec[maxn];
long long fac[maxn], ans;
int n, m;
int ord[maxn], bit;
bool comp(int a, int b) { return rec[a][bit] < rec[b][bit]; }
void mysort(int l, int r, int j) {
  if (l == r) return;
  bit = j;
  sort(ord + l, ord + r + 1, comp);
  int p = l;
  while (p <= r && rec[ord[p]][j] == -1) ++p;
  int cnt = 1;
  for (int i = l + 1; i < p; i++)
    if (rec[ord[i]][j - 1] == rec[ord[i - 1]][j - 1])
      ++cnt;
    else {
      (ans *= fac[cnt]) %= mod;
      cnt = 1;
    }
  (ans *= fac[cnt]) %= mod;
  int t = p;
  while (t <= r) {
    p = t;
    while (p <= r && rec[ord[p]][j] == rec[ord[t]][j]) ++p;
    mysort(t, p - 1, j + 1);
    t = p;
  }
}
int main() {
  fac[0] = 1ll;
  for (int i = 1; i <= N; i++) fac[i] = fac[i - 1] * i % mod;
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= m; i++) rec[i].push_back(0);
  for (int g, i = 1; i <= n; i++) {
    scanf("%d", &g);
    for (int v, j = 1; j <= g; j++) scanf("%d", &v), rec[v].push_back(i);
  }
  for (int i = 1; i <= m; i++) {
    sort(rec[i].begin(), rec[i].end());
    rec[i].push_back(-1);
    ord[i] = i;
  }
  ans = 1ll;
  mysort(1, m, 1);
  printf("%lld\n", ans);
  return 0;
}
