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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 2137
#endif

const uint32_t MOD = 1000000007;
 
struct mint {
  uint32_t val;
 
  mint(ll v = 0) {
    v %= MOD;
    if (v < 0) {
      v += MOD;
    }
    val = v;
  }
 
  mint& operator+=(const mint& other) {
    val += other.val;
    if (val >= MOD) {
      val -= MOD;
    }
    return *this;
  }
 
  mint& operator-=(const mint& other) {
    val += MOD - other.val;
    if (val >= MOD) {
      val -= MOD;
    }
    return *this;
  }
 
  mint& operator*=(const mint& other) {
    val = (uint64_t)val * other.val % MOD;
    return *this;
  }
 
  mint& operator/=(const mint& other) {
    return *this *= other.inv();
  }
 
  friend mint operator+(mint a, const mint& b) { return a += b; }
  friend mint operator-(mint a, const mint& b) { return a -= b; }
  friend mint operator*(mint a, const mint& b) { return a *= b; }
  friend mint operator/(mint a, const mint& b) { return a /= b; }
 
  mint pow(ll exp) const {
    mint a = *this;
    mint res = 1;
    while (exp > 0) {
      if (exp & 1) {
        res *= a;
      }
      a *= a;
      exp /= 2;
    }
    return res;
  }
 
  mint inv() const {
    assert(val != 0);
    return pow(MOD - 2);
  }
 
  friend ostream& operator<<(ostream& os, const mint& m) {
    return os << m.val;
  }
};

int n, k;

namespace wolny {

const int N = 15;
using perm = array<int, N>;
mt19937_64 rng(2137);
uint64_t val[N], idx[N];
int st[2 * N];

struct hsh {
  uint64_t operator()(const perm& v) const {
    uint64_t h = 0;
    for (int i = 0; i < n; i++) {
      h += val[v[i]] ^ idx[i];
    }
    return h;
  }
};

struct eq {
  bool operator()(const perm& a, const perm& b) const {
    for (int i = 0; i < n; i++) {
      if (a[i] != b[i]) {
        return false;
      }
    }
    return true;
  }
};

void rozw() {
  unordered_set<perm, hsh, eq> s;
  for (int i = 0; i < k; i++) {
    perm p;
    for (int j = 0; j < n; j++) {
      cin >> p[j];
      p[j]--;
    }
    s.insert(p);
  }
  while (true) {
    bool any = false;
    for (perm a : s) {
      for (perm b : s) {
        perm c;
        for (int i = 0; i < n; i++) {
          c[i] = a[b[i]];
        }
        if (s.insert(c).second) {
          any = true;
        }
      }
    }
    if (!any) {
      break;
    }
  }
  mint sum = 0;
  for (const perm& p : s) {
    for (int i = 0; i < 2 * n; i++) {
      st[i] = 0;
    }
    for (int i = 0; i < n; i++) {
      int l = n + p[i];
      int r = 2 * n;
      while (l < r) {
        if (l % 2 == 1) {
          sum += st[l++];
        }
        if (r % 2 == 1) {
          sum += st[--r];
        }
        l /= 2;
        r /= 2;
      }
      for (int j = n + p[i]; j > 0; j /= 2) {
        st[j] += 1;
      }
    }
  }
  cout << sum / ssize(s) << '\n';
}

} // namespace wolny

const int N = 3030;
int a[N];
bitset<N> vis[N];

map<int, int> rozklad(int x) {
  map<int, int> ans;
  for (int i = 2; i * i <= x; i++) {
    while (x % i == 0) {
      x /= i;
      ans[i]++;
    }
  }
  if (x > 1) {
    ans[x]++;
  }
  return ans;
}

int pw(int x, int y, int m) {
  int ans = 1 % m;
  while (y > 0) {
    if (y % 2 == 1) {
      ans = (ll)ans * x % m;
    }
    x = (ll)x * x % m;
    y /= 2;
  }
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> k;
  if (k != 1) {
    wolny::rozw();
    return 0;
  }
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  map<int, int> o;
  for (int i = 0; i < n; i++) {
    if (vis[i][i]) {
      continue;
    }
    int sz = 0;
    int j = i;
    do {
      sz++;
      vis[j][j] = true;
      j = a[j];
    } while (j != i);
    for (auto [p, w] : rozklad(sz)) {
      o[p] = max(o[p], w);
    }
  }
  mint l = 1;
  for (auto [x, y] : o) {
    l *= mint(x).pow(y);
  }
  mint s = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (vis[i][j]) {
        continue;
      }
      vector<bool> czy;
      int x = i;
      int y = j;
      do {
        czy.push_back(x > y);
        vis[x][y] = true;
        x = a[x];
        y = a[y];
      } while (x != i || y != j);
      int sz = ssize(czy);
      int r = 1 % sz;
      for (auto [x, y] : o) {
        r = (ll)r * pw(x, y, sz) % sz;
      }
      mint cale = (l - r) / sz;
      vector<int> ps(2 * sz + 1);
      for (int k = 0; k < 2 * sz; k++) {
        ps[k + 1] = ps[k] + czy[k % sz];
      }
      for (int k = 0; k < sz; k++) {
        if (czy[k] == 0) {
          s += cale * ps[sz];
          s += ps[k + r + 1] - ps[k + 1];
        }
      }
    }
  }
  cout << s / l << '\n';
}