let m = 1000000007
let ( +^ ) x y = (x + y) mod m
let ( *^ ) x y = (x * y) mod m

module IntMap = Map.Make (struct
  type t = int
  let compare = compare
end)

let rec drop n = function
  | [] -> []
  | (_ :: t) as l ->
      if n = 0
      then l
      else drop (n - 1) t

let inversion ( <= ) xs =

  let rec merge_count acc xs ys n cnt =
    match xs, ys with
    | _, [] -> cnt, List.rev_append acc xs
    | [], _ -> cnt, List.rev_append acc ys
    | x :: xs', y :: ys' ->
        if x <= y
        then merge_count (x :: acc) xs' ys (n - 1) cnt 
        else merge_count (y :: acc) xs ys' n (n + cnt) in

  let rec sort_count xs = function
    | 0 -> 0, []
    | 1 -> 0, [List.hd xs]
    | n ->
        let c, ys = sort_count xs (n / 2) in
        let d, zs = sort_count (drop (n / 2) xs) ((n + 1) / 2) in
        merge_count [] ys zs (n / 2) (c + d) in

  fst @@ sort_count xs (List.length xs)

let () = Scanf.scanf "%d %d\n" @@ fun n k ->
  let as_ = Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun a -> a in
  Printf.printf "%d\n" @@
  ( +^ ) (k *^ inversion ( <= ) (Array.to_list as_)) @@
  ( *^ ) (k * (k - 1) / 2 mod m) @@
  fst @@
  IntMap.fold (fun _ n (acc, sum) -> (acc + n * sum, sum + n))
    (Array.fold_left (fun m a ->
      IntMap.add a (1 +
        try IntMap.find a m
        with Not_found -> 0) m) IntMap.empty as_) (0, 0)
