base = 10^9 + 7

data BST = Null
         | Node Integer Int BST BST -- ここ以下の要素数、要素、より小さい要素の部分木、より大きい要素の部分木

getN Null = 0
getN (Node n _ _ _) = n

cntAndIns x Null = (0, Node 1 x Null Null)
cntAndIns x (Node n y l r) = case compare x y of
  LT -> let (c,l1) = cntAndIns x l in (c, Node (succ n) y l1 r)
  EQ -> (getN l, Node (succ n) y l r)
  GT -> let (c,r1) = cntAndIns x r in (n - getN r + c, Node (succ n) y l r1)

doCount :: [Int] -> [Integer]
doCount as = tail $ map fst $ scanl (\(_,t) x -> cntAndIns x t) (undefined, Null) as

compute :: Int -> Int -> [Int] -> Integer
compute n k as = foldl1 add $ zipWith f xs ys
  where
    xs = doCount as
    ys = reverse (doCount (reverse as))
    f x y = k * y + k * (k-1) * (x+y) `div` 2
    add x y = (x+y) `mod` base

main = do
  li <- getLine
  let [n,k] = map read $ words li
  li <- getLine
  let as = map read $ words li
  let ans = compute n k as
  print ans
