#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2000200;
const long long MOD = 1e9 + 7;
long long mult(long long a, long long b) { return (a * b) % MOD; }
long long factor[MAXN];
int n, m;
int it;
int num[MAXN];
set<int> sets[MAXN];
int a[MAXN];
int cnt[MAXN];
vector<int> v[MAXN];
int main() {
  factor[0] = 1;
  for (int i = 1; i < MAXN; ++i) factor[i] = mult(factor[i - 1], i);
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; ++i) sets[0].insert(i + 1);
  it = 1;
  for (int i = 0; i < n; ++i) {
    int k;
    scanf("%d", &k);
    for (int i = 0; i < k; ++i) {
      scanf("%d", &a[i]);
      cnt[a[i]]++;
    }
    for (int i = 0; i < k; ++i) v[cnt[a[i]]].push_back(a[i]);
    for (int i = 0; i < k; ++i) {
      if (v[cnt[a[i]]].empty()) continue;
      map<int, set<int>> inter;
      for (int j : v[cnt[a[i]]]) {
        int x = num[j];
        {
          inter[x].insert(j);
          sets[x].erase(j);
        }
      }
      for (auto x : inter) {
        for (auto j : x.second) num[j] = it;
        sets[it++] = x.second;
      }
      v[cnt[a[i]]].clear();
    }
    for (int i = 0; i < k; ++i) cnt[a[i]] = 0;
  }
  long long ans = 1;
  for (int i = 0; i < it; ++i) ans = mult(ans, factor[sets[i].size()]);
  printf("%lld", ans);
  return 0;
}
