#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   #pragma GCC optimize( unroll-loops )   using namespace std;   using ll = long long;   using db = long double;   using ii = pair<int, int>;   const int N = 1e7 + 5, LG = 19, MOD = 1e9 + 7;   const int SQ = 225;   const long double EPS = 1e-7;   int fast(int b, int e) {     int res = 1;     for (; e; e >>= 1, b = 1ll * b * b % MOD)       if (e & 1) res = 1ll * res * b % MOD;     return res;   }   vector<int> adj[10005];   int n, k;   map<int, map<int, ll>> mat;   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n >> k;     vector<int> deg(n + 1);     for (int i = 1; i <= k; i++)       for (int j = i + 1; j <= k; j++) {         adj[j].push_back(i);         deg[j]++;         deg[i]++;       }     for (int i = k + 1; i <= n; i++) {       for (int j = 0; j < k; j++) {         int x;         cin >> x;         adj[i].push_back(x);         deg[i]++;         deg[x]++;       }     }     for (int i = 1; i <= n; i++) {       mat[i][i] = deg[i];       for (auto x : adj[i]) {         mat[i][x] = MOD - 1;         mat[x][i] = MOD - 1;       }     }     ll ans = 1;     for (int i = n; i >= 2; --i) {       ll v = mat[i][i];       ll inv = fast(MOD - v, MOD - 2);       ans = 1ll * ans * v % MOD;       for (auto j : adj[i]) {         ll coff = inv * mat[j][i] % MOD;         for (auto k : adj[i]) {           mat[j][k] = (mat[j][k] + mat[i][k] * coff) % MOD;         }       }     }     cout << ans <<   n ;     return 0;   }   