#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ add(long long, int);
int /*<<< orphan*/ printf(char *, long long);
int /*<<< orphan*/ scanf(char *, long long *);
long long sum(long long);
int main(int argc, const char *argv[]) {
  long long i, j, n, last = -1, ret = 0, continuing = 1;
  scanf("%lli", &n);
  for (i = 1; i <= n; ++i) {
    scanf("%lli", &j);
    if (j < last) {
      ++continuing;
      if (continuing > 2)
        ret -= continuing - 1;
    } else
      continuing = 1;
    add(j, 1);
    ret += i - sum(j);
    last = j;
  }
  printf("%lli", ret);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0xffffffffffffffff,-0x30(%rbp)
ff
movq   $0x0,-0x38(%rbp)
movq   $0x1,-0x40(%rbp)
lea    0xe8b(%rip),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <scanf@plt>
movq   $0x1,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x28(%rbp),%rax
jg     1233 <main+0xf3>
lea    0xe65(%rip),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <scanf@plt>
mov    -0x20(%rbp),%rax
cmp    -0x30(%rbp),%rax
jge    11e1 <main+0xa1>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x2,-0x40(%rbp)
jle    11dc <main+0x9c>
mov    -0x40(%rbp),%rcx
sub    $0x1,%rcx
mov    -0x38(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x38(%rbp)
jmp    11e9 <main+0xa9>
movq   $0x1,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   1030 <add@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <sum@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
sub    %rcx,%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1186 <main+0x46>
mov    -0x38(%rbp),%rsi
lea    0xdc2(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xeab(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1060 <scanf@plt>
cmpq   $0x0,0x8(%rsp)
jle    120f <main+0xcf>
mov    $0xffffffffffffffff,%r14
xor    %ebx,%ebx
mov    $0x1,%ebp
xor    %ecx,%ecx
mov    $0x1,%r13d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rcx,0x10(%rsp)
lea    0xe64(%rip),%rdi
mov    %rsp,%rsi
call   1060 <scanf@plt>
mov    (%rsp),%rdi
lea    0x1(%rbp),%r12
cmp    $0x2,%rbp
mov    $0x0,%eax
cmovl  %rax,%rbp
cmp    %r14,%rdi
mov    %rbp,%r15
cmovge %rax,%r15
mov    $0x1,%eax
cmovge %rax,%r12
mov    $0x1,%esi
call   1030 <add@plt>
mov    (%rsp),%rdi
call   1050 <sum@plt>
mov    0x10(%rsp),%rsi
lea    (%rsi,%r13,1),%rcx
sub    %r15,%rcx
sub    %rax,%rcx
mov    (%rsp),%r14
add    $0x1,%r13
add    $0x1,%rbx
mov    %r12,%rbp
cmp    0x8(%rsp),%rbx
jl     1190 <main+0x50>
sub    %r15,%rsi
sub    %rax,%rsi
add    %rbx,%rsi
jmp    1211 <main+0xd1>
xor    %esi,%esi
lea    0xde8(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%lli\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 -1, i64* %9
store i64 0, i64* %10
store i64 1, i64* %11
%12 = call i32 @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64* %8)
store i64 1, i64* %6
br label %13
13: ; preds = %44, %2
%14 = load i64, i64* %6
%15 = load i64, i64* %8
%16 = icmp sle i64 %14, %15
br i1 %16, label %17, label %47
17: ; preds = %13
%18 = call i32 @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64* %7)
%19 = load i64, i64* %7
%20 = load i64, i64* %9
%21 = icmp slt i64 %19, %20
br i1 %21, label %22, label %33
22: ; preds = %17
%23 = load i64, i64* %11
%24 = add nsw i64 %23, 1
store i64 %24, i64* %11
%25 = load i64, i64* %11
%26 = icmp sgt i64 %25, 2
br i1 %26, label %27, label %32
27: ; preds = %22
%28 = load i64, i64* %11
%29 = sub nsw i64 %28, 1
%30 = load i64, i64* %10
%31 = sub nsw i64 %30, %29
store i64 %31, i64* %10
br label %32
32: ; preds = %27, %22
br label %34
33: ; preds = %17
store i64 1, i64* %11
br label %34
34: ; preds = %33, %32
%35 = load i64, i64* %7
%36 = call i32 @add(i64 %35, i32 1)
%37 = load i64, i64* %6
%38 = load i64, i64* %7
%39 = call i64 @sum(i64 %38)
%40 = sub nsw i64 %37, %39
%41 = load i64, i64* %10
%42 = add nsw i64 %41, %40
store i64 %42, i64* %10
%43 = load i64, i64* %7
store i64 %43, i64* %9
br label %44
44: ; preds = %34
%45 = load i64, i64* %6
%46 = add nsw i64 %45, 1
store i64 %46, i64* %6
br label %13
47: ; preds = %13
%48 = load i64, i64* %10
%49 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 %48)
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"%lli\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i64
%4 = alloca i64
%5 = bitcast i64* %3 to i8*
%6 = bitcast i64* %4 to i8*
%7 = call i32 @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64* %4)
%8 = load i64, i64* %4
%9 = icmp slt i64 %8, 1
br i1 %9, label %33, label %10
10: ; preds = %2, %10
%11 = phi i64 [ %22, %10 ], [ 1, %2 ]
%12 = phi i64 [ %28, %10 ], [ 0, %2 ]
%13 = phi i64 [ %29, %10 ], [ -1, %2 ]
%14 = phi i64 [ %30, %10 ], [ 1, %2 ]
%15 = call i32 @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64* %3)
%16 = load i64, i64* %3
%17 = icmp slt i64 %16, %13
%18 = add nsw i64 %11, 1
%19 = icmp sgt i64 %11, 1
%20 = select i1 %17, i1 %19, i1 false
%21 = select i1 %20, i64 %11, i64 0
%22 = select i1 %17, i64 %18, i64 1
%23 = call i32 @add(i64 %16, i32 1)
%24 = load i64, i64* %3
%25 = call i64 @sum(i64 %24)
%26 = add i64 %12, %14
%27 = add i64 %21, %25
%28 = sub i64 %26, %27
%29 = load i64, i64* %3
%30 = add nuw nsw i64 %14, 1
%31 = load i64, i64* %4
%32 = icmp slt i64 %14, %31
br i1 %32, label %10, label %33
33: ; preds = %10, %2
%34 = phi i64 [ 0, %2 ], [ %28, %10 ]
%35 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 %34)
ret i32 0
}