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
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define ll long long
#define ld long double
#define ull unsigned long long
void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)

ll MOD = 1000000007;

ll power(ll a, ll n, ll m){
  ll res = 1;
  while(n){
    if(n&1) res = (res*a) % m;
    n/=2;
    a = (a*a) % m;
  }
  return res;
}

void solve()
{
  ll n,k;
  cin >> n >> k;
  vector<vector<ll>> list;
  map<vector<ll>, bool> mp;
  for(ll i = 0; i < k; i++){
    vector<ll> tmp(n);
    for(auto &el : tmp) cin >> el;
    list.push_back(tmp);
    mp[tmp] = true;
  }

  while(true){
    ll listSize = list.size();
    for(ll i = 0; i < listSize; i++){
      for(ll j = 0; j < listSize; j++){
        vector<ll> tmp(n);
        for(ll x = 0; x < n; x++){
          tmp[list[i][x]-1] = list[j][x];
        }
        if(mp[tmp]) continue;
        list.push_back(tmp);
        mp[tmp] = true;
      }
    }
    if(listSize == list.size()) break;
  }
  ll res = 0;
  for(auto sub : list){
    for(ll i = 0; i < n; i++){
      for(ll j = i+1; j < n; j++){
        if(sub[i] > sub[j]) res++;
        res%=MOD;
      }
    }
  }

  cout << res*power((ll)list.size(), MOD-2, MOD)%MOD << endl;
} 
 
int main()
{

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

// #ifndef ONLINE_JUDGE
//   freopen("../../in.in", "r", stdin);
//   freopen("../../out.out", "w", stdout);
// #endif
  
  solve();
  
}