#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 100;
const int mod = 1e9 + 7;
int p[N], n, m;
vector<int> g[N];
int calc[N], a[N];
long long res = 1;
int find(int v) {
  if (v == p[v]) return v;
  p[v] = find(p[v]);
  return p[v];
}
void unite(int v, int u) {
  v = find(v);
  u = find(u);
  if (rand() & 1) swap(v, u);
  if (v != u) {
    p[v] = u;
    a[u] += a[v];
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 1, q, r; i <= n; i++) {
    cin >> r;
    for (int j = 1; j <= r; j++) {
      cin >> q;
      g[q].push_back(i);
    }
  }
  for (int i = 1; i <= m; i++) p[i] = i, a[i] = 1;
  sort(g + 1, g + m + 1);
  for (int i = 1; i < m; i++) {
    if (g[i] == g[i + 1]) {
      unite(i, i + 1);
    }
  }
  calc[0] = 1;
  for (int i = 1; i < N; i++) calc[i] = (calc[i - 1] * 1ll * i) % mod;
  for (int i = 1; i <= m; i++) {
    if (find(i) == i) res = (res * 1ll * calc[a[find(i)]]) % mod;
  }
  cout << res;
  return 0;
}
