#line 2 "lib/prelude.hpp" #include using namespace std; using ll = long long; #define rep2(i, m, n) for (auto i = (m); i < (n); i++) #define rep(i, n) rep2(i, 0, n) #define repr2(i, m, n) for (auto i = (n); i-- > (m);) #define repr(i, n) repr2(i, 0, n) #define all(x) begin(x), end(x) #line 2 "main.cpp" #include using mint = atcoder::modint1000000007; vector conv(vector a, vector b, int deg = -1) { if (~deg) a.resize(min(a.size(), deg)), b.resize(min(b.size(), deg)); auto f = convolution(a, b); if (~deg) f.resize(deg); return f; } // vector exp(vector h, int deg) { // assert(h[0] == 0); // int n = 1 << atcoder::internal::ceil_pow2(deg); // h.resize(n); // vector f = {1}, g = {1}; // vector q(h.size() - 1); // rep(i, q.size()) q[i] = h[i + 1] * (i + 1); // for (int m = 1; m < n; m *= 2) { // auto fgg = conv(conv(f, g, m), g, m); // g.resize(m); // rep(i, m) g[i] = g[i] * 2 - fgg[i]; // auto fq = conv(f, {q.begin(), q.begin() + m - 1}); // rep(i, fq.size()) fq[i] = // -fq[i] + (i < f.size() - 1 ? f[i + 1] * (i + 1) : 0); // auto w = conv(g, fq, m * 2 - 1); // rep(i, m - 1) w[i] += q[i]; // w.push_back(0); // repr(i, m * 2 - 1) w[i + 1] = h[i + 1] - w[i] / (i + 1); // w[0] = h[0]; // auto fw = conv(f, w, m * 2); // f.resize(m * 2); // rep(i, m * 2) f[i] += fw[i]; // } // return f; // } int N, M, A[1000]; bool used[1000]; int cycles[1001]; int main() { scanf("%d%d", &N, &M); rep(i, N) scanf("%d", A+i), A[i]--; rep(i, N) if (!used[i]) { int cnt = 0; for (int v = i; !used[v]; v = A[v]) used[v] = true, cnt++; cycles[cnt]++; } mint ans = 1; rep(m, N+1) if (cycles[m] != 0) { int l = cycles[m], g = gcd(M, m); vector poly(l+1); rep2(k, 1, l+1) if (M % k == 0 && k % g == 0) { poly[k] = mint(m).pow(k-1) / k; } vector po = {1}; vector exp(l+1, 0); mint ifact = 1; rep(i, l) { po = conv(move(po), poly, l+1); ifact /= i+1; rep(j, l+1) exp[j] += po[j] * ifact; } ans *= exp[l] / ifact; } printf("%d\n", ans.val()); }