#include <bits/stdc++.h>
const long double PI = acos(-1);
const long double eps = 0.0000000001;
const long long INF = 0x3fffffffffffffff;
const long long MOD = 1000000007;
long long n, k, ans = 1;
std::map<long long, long long> v[10005];
long long power(long long A, long long B) {
  long long res = 1;
  while (B) {
    if (B & 1) res = res * A % MOD;
    B >>= 1;
    A = A * A % MOD;
  }
  return res;
}
signed main() {
  std::ios::sync_with_stdio(false);
  std::cin >> n >> k;
  for (long long i = 0; i != k; ++i)
    for (long long j = i + 1; j != k; ++j) {
      ++v[i][i];
      ++v[j][j];
      --v[i][j];
      --v[j][i];
    }
  for (long long i = k; i != n; ++i) {
    for (long long j = 0; j != k; ++j) {
      static long long x;
      std::cin >> x;
      --x;
      ++v[i][i];
      ++v[x][x];
      --v[i][x];
      --v[x][i];
    }
  }
  for (long long i = 1; i != n; ++i) v[i].erase(0);
  v[0].clear();
  for (long long i = 1; i != n; ++i)
    for (auto &j : v[i]) j.second = (j.second % MOD + MOD) % MOD;
  for (long long i = n; --i;) {
    for (long long j = i; --j;)
      if (v[j].find(i) != v[j].end()) {
        long long tmp = (MOD - v[j][i]) * power(v[i][i], MOD - 2) % MOD;
        for (auto k : v[i])
          v[j][k.first] = (v[j][k.first] + k.second * tmp) % MOD;
        v[j].erase(i);
      }
  }
  for (long long i = 1; i != n; ++i) ans = ans * v[i][i] % MOD;
  std::cout << ans << std::endl;
  return 0;
}
