#include <bits/stdc++.h>
using namespace std;
inline char nc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline void read(int &x) {
  char c = nc(), b = 1;
  for (; !(c >= '0' && c <= '9'); c = nc())
    if (c == '-') b = -1;
  for (x = 0; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = nc())
    ;
  x *= b;
}
const int N = 10005;
const int P = 1e9 + 7;
inline long long Pow(long long a, int b) {
  long long ret = 1;
  for (; b; b >>= 1, a = a * a % P)
    if (b & 1) ret = ret * a % P;
  return ret;
}
inline long long Inv(long long a) { return Pow(a, P - 2); }
int n, K;
map<int, int> a[N];
int lst[N], pnt;
inline void gauss(int i, int j) {
  long long t = Inv(a[i][i]) * a[j][i] % P;
  for (map<int, int>::iterator it = a[i].begin(); it != a[i].end(); it++)
    if (it->first < n) {
      int k = it->first;
      (a[j][k] += P - t * a[i][k] % P) %= P;
    }
  pnt = 0;
  for (map<int, int>::iterator it = a[j].begin(); it != a[j].end(); it++)
    if (it->first < n && !(it->second)) lst[++pnt] = it->first;
  for (int i = 1; i <= pnt; i++) a[j].erase(a[j].find(lst[i]));
}
inline int det() {
  for (int i = 1; i < n; i++)
    for (map<int, int>::iterator it = a[i].begin(); it != a[i].end(); it++)
      if (it->first != i && it->first < n) gauss(i, it->first);
  long long ret = 1;
  for (int i = 1; i < n; i++) ret = ret * a[i][i] % P;
  return ret;
}
int main() {
  int x;
  read(n);
  read(K);
  for (int i = n - K; i; i--)
    for (int j = 1; j <= K; j++)
      read(x), x = n - x + 1, a[i][x] = a[x][i] = P - 1, a[i][i]++, a[x][x]++;
  for (int i = n - K + 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++)
      a[i][j] = a[j][i] = P - 1, a[i][i]++, a[j][j]++;
  printf("%d\n", det());
  return 0;
}
