program kleene_inversion
  implicit none
  integer(8), parameter :: md = 1000000007_8
  integer :: n, a(2000) = 0, b(4000) = 0, i, j
  integer(8) :: inv(4000) = 0_8, k, ans = 0_8, coef
  read(*,*) n, k
  read(*,*) a(1:n)
  b(1:n) = a(1:n)
  b(n+1:2*n) = a(1:n)
  do i = 1, 2*n
    do j = 1, i-1
      if (b(j) > b(i)) inv(i) = inv(i)+1_8
    end do
  end do
  coef = mod(mod(k*(k-1_8),md)*500000004_8,md)
  do i = 1, n
    ans = mod(ans+mod(k*inv(i),md)+mod(coef*(inv(n+i)-inv(i)),md),md)
  end do
  write(*,'(i0)') mod(ans,md)
end program kleene_inversion