let rec tabulate n f = 
  let rec loop acc m =
    if m < n then loop (f m :: acc) (m + 1)
    else List.rev acc in
  loop [] 0

let () =
  let n, _ = Scanf.scanf "%d %d\n" (fun n t -> n, t) in
  tabulate n (fun _ -> Scanf.scanf "%d " (fun a -> a))
    |> List.fold_left (fun (acc, min, mins, maxs, pred) a ->
        if a < pred then ((min, pred, mins, maxs) :: acc, a, 0, 0, a)
        else (acc, min, (if a = min then mins + 1 else mins), (if a = pred then maxs + 1 else 0) , a)) ([], max_int, 0, 0, max_int)
    |> (fun (acc, min, mins, maxs, pred) -> (min, pred, mins, maxs) :: acc)
    |> (fun ranges ->
        let max = List.fold_left max 0 (List.map (fun (min, pred, _, _) -> pred - min) ranges) in
        List.filter (fun (min, pred, _, _) -> pred - min = max) ranges)
    |> List.map (fun (_, _, mins, maxs) -> min mins maxs + 1)
    |> List.fold_left ( + ) 0
    |> Printf.printf "%d\n"
