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
#include <bits/stdc++.h>
using namespace std;

#pragma GCC maksuje_zadania_na_essie("pot200");
#define loop(i, a, b) for(int i = a; i <= b; i++)
#define loop_rev(i, a, b) for(int i = a; i >= b; i--)
#define all(x) x.begin(), x.end()
#define sz(x) int(x.size())
#define pb push_back

using ll = int64_t;
using ull = uint64_t;
using uint = uint32_t;

class Permutation {
	vector<int> perm;
	public:
		Permutation(int n) {
			perm.resize(n);
		}
		Permutation(vector<int>&& _perm) {
			this->perm = _perm;
		}
		static Permutation neutral(int n) {
			Permutation res(n);
      loop(i, 0, n-1) res[i] = (i+1);
      return res;
		}
		Permutation operator*(const Permutation& other) const {
			Permutation res(size());
			loop(i, 0, sz(perm)-1) {
				res.perm[i] = perm[other.perm[i]-1];
			}
			return res;
		}
		inline int operator[](int i) const {
			return perm[i-1];
		}
		inline int& operator[](int i) {
			return perm[i-1];
		}
		int size() const {
			return sz(perm);
		}
		bool operator==(const Permutation& other) const {
			return (perm == other.perm);
		}
		friend ostream& operator<<(ostream& os, const Permutation& p) {
			os << "{ ";
			loop(i, 0, sz(p.perm)-1) {
				if(i != 0) os << ", ";
				os << p.perm[i];
			}
			os << " }";
			return os;
		}
    friend istream& operator>>(istream& is, Permutation& p) {
      loop(i, 0, sz(p)-1) is >> p.perm[i];
      return is;
    }
};

constexpr ll MOD = 1e9 + 7;

ll qpow(ll a, ll b) {
  ll res = 1;
  while(b) {
    if(b&1) res = (res * a) % MOD;
    a = (a * a) % MOD;
    b /= 2;
  }
  return res;
}

ll fact(ll x) {
  ll res = 1;
  loop(i, 1, x) {
    res = (res * i) % MOD;
  }
  return res;
}

ll dp(int n) {
  ll res = 0;
  loop(i, 2, n) {
    res = (res * i + i * (i - 1) / 2 * fact(i - 1)) % MOD;
  }
  return res;
}

bool generates(Permutation const& perm, Permutation const& b) {
  Permutation temp = perm;
  do {
    temp = temp * perm;
    if(temp == b) return true;
  } while(temp != perm);
  return false;
}

int main() {
  cin.tie(0)->sync_with_stdio(false);
  int n, k; cin >> n >> k;

  Permutation perm(n); cin >> perm;

  vector<Permutation> all_perms;

  
  loop(i, 0, k-2) {
    Permutation new_perm(n); cin >> new_perm;
    if(generates(perm, new_perm)) {
      continue;
    }
    if(generates(new_perm, perm)) {
      perm = new_perm;
      continue;
    }
    cout << (dp(n) * qpow(fact(n), MOD-2)) % MOD << '\n';
    return 0;
  }

  vector<int> cnt(n+1); ll res = 0;
  Permutation p = perm; ll ord = 0;

  do {
    p = p * perm; ++ord;
    //////////////////////////
    loop(i, 1, n) {
      loop(j, p[i]+1, n) res += cnt[j];
      ++cnt[p[i]];
    }
    loop(i, 1, n) {
      --cnt[p[i]];
    }
    //////////////////////////
  } while(p != perm);

  cout << (res * qpow(ord % MOD, MOD - 2)) % MOD << '\n';

}