1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#ifdef LOCAL
class SS {
   int x[0];
};
extern "C" SS __sanitizer_print_stack_trace();
#define __last_state                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   \
   }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   \
   ;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   \
   SS x {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              \
      __sanitizer_print_stack_trace()
#endif
#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <math.h>
using namespace __gnu_pbds;
using namespace std;
#define endl "\n"
#define mp make_pair
#define st first
#define nd second
#define pii pair<int, int>
#define vi vector<int>
#define pb push_back
#define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10), cin.tie(0), cout.tie(0);
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define fwd(i, a, b) for (int i = (a); i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(c) (c).begin(), (c).end()
#define sz(X) (int)((X).size())
typedef long double ld;
typedef unsigned long long ull;
typedef long long ll;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set; // find_by_order(x) <-returns x-th element   order_of_key(x) <- returns order of element x
// mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());uniform_int_distribution<int> distr(0, 1e9);auto my_rand = bind(distr, gen); // my_rand() zwraca teraz liczbe z przedzialu [a, b]
#ifdef LOCAL
ostream &operator<<(ostream &out, string str) {
   for (char c : str)
      out << c;
   return out;
}
template <class L, class R> ostream &operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.st << ", " << p.nd << ")"; }
template <class L, class R, class S> ostream &operator<<(ostream &out, tuple<L, R, S> p) {
   auto &[a, b, c] = p;
   return out << "(" << a << ", " << b << ", " << c << ")";
}
template <class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) {
   out << '{';
   for (auto it = a.begin(); it != a.end(); it = next(it))
      out << (it != a.begin() ? ", " : "") << *it;
   return out << '}';
}
void dump() { cerr << "\n"; }
template <class T, class... Ts> void dump(T a, Ts... x) {
   cerr << a << ", ";
   dump(x...);
}
#define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__)
#else
#define debug(...) ;
#endif

#define int long long
const ll mod = 1e9 + 7;

ull mod_pow(ull b, ull e) {
   ull ans = 1;
   for (; e; b = b * b % mod, e /= 2)
      if (e & 1)
         ans = ans * b % mod;
   return ans;
}

ull inv(ull a) { return mod_pow(a, mod - 2); }

const int MAXN = 3e3 + 99;

int I[MAXN * MAXN];

int get_inv(int x) {
   if (I[x])
      return I[x];
   else
      return I[x] = inv(x);
}

typedef vector<vi> vvi;
typedef vector<pii> vpi;

int vis[MAXN][MAXN];

void split(vi p, int iter, auto f) {
   int n = sz(p);
   rep(i, n) rep(j, n) if (i != j and vis[i][j] != iter) {
      pii ij = {i, j};
      pii x = ij;
      do {
         vis[x.st][x.nd] = iter;
         x = {p[x.st], p[x.nd]};
         f(ij, x);
      } while (x != ij);
   }
}

int e[MAXN * MAXN];
void init() { rep(i, MAXN * MAXN) e[i] = -1; }

int findd(int x) { return e[x] < 0 ? x : e[x] = findd(e[x]); }
bool sameSet(int a, int b) { return findd(a) == findd(b); }
int size(int x) { return -e[findd(x)]; }
bool join(int a, int b) {
   a = findd(a), b = findd(b);
   if (a == b)
      return false;
   if (e[a] > e[b])
      swap(a, b);
   e[a] += e[b];
   e[b] = a;
   return true;
}

int R[MAXN * MAXN][2];

int32_t main() {
   _upgrade;

   int n, k;
   cin >> n >> k;

   debug("WTF");
   init();
   auto to_idx = [n](const pii &x) { return x.st + n * x.nd; };
   auto from_idx = [n](int p) { return make_pair(p % n, p / n); };
   rep(iter, k) {
      vi p(n);
      for (auto &i : p) {
         cin >> i;
         --i;
      }
      debug(p);
      split(p, iter + 1, [&](auto a, auto b) { join(to_idx(a), to_idx(b)); });
   }

   int res = 0;

   rep(i, n * n) {
      auto [x, y] = from_idx(i);
      if (x != y)
         R[findd(i)][x < y]++;
   }
   rep(i, MAXN * MAXN) {
      auto &r = R[i];
      if (r[0] and r[1]) {
         debug(r[0], r[1]);
         res += ((r[0] * r[1]) % mod) * get_inv(r[0] + r[1]) % mod;
      }
   }

   //    debug(X);
   //    rep(i, n) rep(j, i) res += X[j][i];
   cout << res % mod << endl;
}