#undef NDEBUG #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i)) #define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i)) #define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i)) #if defined(_MSC_VER) || __cplusplus > 199711L #define aut(r,v) auto r = (v) #else #define aut(r,v) __typeof(v) r = (v) #endif #define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it) #define all(o) (o).begin(), (o).end() #define pb(x) push_back(x) #define mp(x,y) make_pair((x),(y)) #define mset(m,v) memset(m,v,sizeof(m)) #define INF 0x3f3f3f3f #define INFL 0x3f3f3f3f3f3f3f3fLL using namespace std; typedef vector vi; typedef pair pii; typedef vector > vpii; typedef long long ll; template inline void amin(T &x, U y) { if(y < x) x = y; } template inline void amax(T &x, U y) { if(x < y) x = y; } #ifdef NDEBUG #error assert is disabled! #endif template T readNatural(T lo, T up) { assert(0 <= lo && lo <= up); T x = 0; while(1) { int d = getchar(); if(!('0' <= d && d <= '9')) { ungetc(d, stdin); break; } d -= '0'; assert(d <= up && x <= (up - d) / 10); x = x * 10 + d; } assert(lo <= x && x <= up); return x; } void readSpace() { int c = getchar(); assert(c == ' '); } static bool read_eof = false; void readEOL() { int c = getchar(); if(c == EOF) { assert(!read_eof); read_eof = true; }else { assert(c == '\n'); } } template struct ModInt { static const int Mod = MOD; unsigned x; ModInt(): x(0) { } ModInt(signed sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; } ModInt(signed long long sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; } int get() const { return (int)x; } ModInt &operator+=(ModInt that) { if((x += that.x) >= MOD) x -= MOD; return *this; } ModInt &operator-=(ModInt that) { if((x += MOD - that.x) >= MOD) x -= MOD; return *this; } ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; } ModInt &operator/=(ModInt that) { return *this *= that.inverse(); } ModInt operator+(ModInt that) const { return ModInt(*this) += that; } ModInt operator-(ModInt that) const { return ModInt(*this) -= that; } ModInt operator*(ModInt that) const { return ModInt(*this) *= that; } ModInt operator/(ModInt that) const { return ModInt(*this) /= that; } ModInt inverse() const { signed a = x, b = MOD, u = 1, v = 0; while(b) { signed t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); } if(u < 0) u += Mod; ModInt res; res.x = (unsigned)u; return res; } }; typedef ModInt<1000000007> mint; struct FenwickTree { typedef int T; vector v; void init(int n) { v.assign(n, 0); } void add(int i, T x) { for(; i < (int)v.size(); i |= i+1) v[i] += x; } T sum(int i) const { //[0, i) T r = 0; for(-- i; i >= 0; i = (i & (i+1)) - 1) r += v[i]; return r; } T sum(int left, int right) const { //[left, right) return sum(right) - sum(left); } }; //0 <= res[i] < n - i void permutationToFactorialNumberSystem(const vector &p, vector &res) { int n = p.size(); FenwickTree ft; ft.init(n); res.resize(n); for(int i = n - 1; i >= 0; -- i) { res[i] = ft.sum(p[i]); ft.add(p[i], 1); } } long long integerToFactorialNumberSystem(int n, long long x, vector &res) { assert(x >= 0); res.resize(n); for(int i = n - 1; i >= 0; -- i) { res[i] = x % (n - i); x /= n - i; } return x; } int addFactorialNumberSystem(const vector &x, const vector &y, vector &res) { int n = x.size(); assert(y.size() == n); res.resize(n); int carry = 0; for(int i = n - 1; i >= 0; -- i) { int z = x[i] + y[i] + carry; res[i] = z % (n - i); carry = z / (n - i); } return carry; } //sum [l,u) ll arithsum(int l, int u) { if(l >= u) return 0; int w = u - l; return (ll)l * w + (ll)w * (w-1) / 2; } struct DP { vector > dp; DP(int n): dp((n+1) * 2) { } pair &operator()(int i, bool lt) { return dp[i * 2 + lt]; } }; // p < x となる置換のinvの和 //= p < x となるfactorial number systemの桁の和の和 //(i, lt)に対して(cnt, sum)を持つ桁DPをする mint solve(const vector &x) { int n = x.size(); DP dp(n); dp(n, true) = mp(mint(1), mint(0)); for(int i = n-1; i >= 0; -- i) rep(lt, 2) { mint cnt, sum; int d = x[i]; /* //以下と同じことをまとめて数えてやる rep(e, n - i) if(lt || e <= d) { auto p = dp(i+1, lt || e < d); cnt += p.first, sum += p.second; sum += p.first * e; } */ //e < d の場合 { auto p = dp(i+1, true); cnt += p.first * d, sum += p.second * d; sum += p.first * arithsum(0, d); } //e = d の場合 { auto p = dp(i+1, lt != 0); cnt += p.first, sum += p.second; sum += p.first * d; } //e > d の場合 if(lt) { auto p = dp(i+1, true); int t = n - i - d - 1; cnt += p.first * t, sum += p.second * t; sum += p.first * arithsum(d+1, n - i); } dp(i, lt != 0) = mp(cnt, sum); } return dp(0, false).second; } int main() { int N; long long K; N = readNatural(1, (int)1e5); readSpace(); K = readNatural(0LL, (ll)1e18); readEOL(); vector p(N); rep(i, N) { p[i] = readNatural(1, N); if(i < N-1) readSpace(); else readEOL(); } assert(!read_eof); { vector vis(N); rep(i, N) { assert(!vis[p[i]-1]); vis[p[i]-1] = true; } } rep(i, N) -- p[i]; //p, f^K(p) を factorial number system に変換する //キャリーは覚えておいて、それは別に足す vector x, y, z; permutationToFactorialNumberSystem(p, x); long long cycles = 0; cycles += integerToFactorialNumberSystem(N, K, y); cycles += addFactorialNumberSystem(x, y, z); //factorial number systemでの要素ごとのinvへの付与を考えると、 // \sum_{p is a permutation of [1..n]} inv(p) //= \sum_{i=2}^n \sum{j=0}^{i-1} (n! / i) j //= n! \sum_{i=1}^{n-1} i / 2 //= n! n (n-1) / 4 mint allinvsum = 1; rep(i, N) allinvsum *= i+1; allinvsum *= mint(N) * (N-1) / 4; mint ans; ans += allinvsum * cycles; ans += solve(z); ans -= solve(x); printf("%d\n", ans.get()); return 0; }