program sample
    implicit none
  
    integer(8) :: i,j,m,n,x(2000),b,c,d,s,t,k
    integer(8),allocatable :: a(:),y(:)
  
    read(*,*) n,k
    allocate(a(n))
    read(*,*)a
    s=0
    t=0
    x(:)=0
    do i=1,n
        b=a(i)
        c=0
        x(b)=x(b)+1
        if(b>=2)then
            do j=1,b-1
                c=c+x(j)
            end do
        end if
        s=s+c
        t=t+i-x(b)-c
    end do
    d=10**9+7
    m=mod(k*(k-1)/2,d)
    
    s=mod(s*m,d)
    t=mod(t*(m+k),d)
    m=mod(s+t,d)
   

    write(*,*) m
  
    stop
end program sample
  

