#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(int, int);
int /*<<< orphan*/ freopen(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ scanf(char *, int *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int sum(int);
int main(int argc, const char *argv[]) {
  freopen("rest.in", "r", stdin);
  freopen("rest.out", "w", stdout);
  int i, j, n, last = -1, ret = 0, continuing = 1;
  scanf("%i", &n);
  for (i = 1; i <= n; ++i) {
    scanf("%i", &j);
    if (j < last) {
      ++continuing;
      if (continuing > 2)
        ret -= continuing - 1;
    } else
      continuing = 1;
    add(j, 1);
    ret += i - sum(j);
    last = j;
  }
  printf("%i", ret);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%edx
lea    0xe8a(%rip),%rdi
lea    0xe8b(%rip),%rsi
call   1050 <freopen@plt>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edx
lea    0xe78(%rip),%rdi
lea    0xe7a(%rip),%rsi
call   1050 <freopen@plt>
movl   $0xffffffff,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x1,-0x28(%rbp)
lea    0xe5b(%rip),%rdi
lea    -0x1c(%rbp),%rsi
call   1070 <scanf@plt>
movl   $0x1,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jg     125b <main+0x10b>
lea    0xe38(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <scanf@plt>
mov    -0x18(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    1218 <main+0xc8>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x2,-0x28(%rbp)
jle    1213 <main+0xc3>
mov    -0x28(%rbp),%ecx
sub    $0x1,%ecx
mov    -0x24(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x24(%rbp)
jmp    121f <main+0xcf>
movl   $0x1,-0x28(%rbp)
mov    -0x18(%rbp),%edi
mov    $0x1,%esi
call   1030 <add@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%edi
call   1060 <sum@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
sub    %ecx,%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11ca <main+0x7a>
mov    -0x24(%rbp),%esi
lea    0xdb0(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e83(%rip),%rax
mov    (%rax),%edx
lea    0xe92(%rip),%rdi
lea    0xe93(%rip),%rsi
call   1050 <freopen@plt>
mov    0x2e57(%rip),%rax
mov    (%rax),%edx
lea    0xe80(%rip),%rdi
lea    0xe82(%rip),%rsi
call   1050 <freopen@plt>
lea    0xe78(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1070 <scanf@plt>
cmpl   $0x0,0xc(%rsp)
jle    124c <main+0xfc>
mov    $0xffffffff,%r14d
xor    %ebp,%ebp
mov    $0x1,%r15d
xor    %ecx,%ecx
mov    $0x1,%r12d
nopl   0x0(%rax,%rax,1)
mov    %ecx,%eax
mov    %rax,0x10(%rsp)
lea    0xe37(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1070 <scanf@plt>
mov    0x8(%rsp),%edi
lea    0x1(%r15),%r13d
cmp    $0x2,%r15d
mov    $0x0,%eax
cmovl  %eax,%r15d
cmp    %r14d,%edi
mov    %r15d,%ebx
cmovge %eax,%ebx
mov    $0x1,%eax
cmovge %eax,%r13d
mov    $0x1,%esi
call   1030 <add@plt>
mov    0x8(%rsp),%edi
call   1060 <sum@plt>
mov    0x10(%rsp),%rsi
lea    (%rsi,%r12,1),%ecx
sub    %ebx,%ecx
sub    %eax,%ecx
mov    0x8(%rsp),%r14d
add    $0x1,%r12d
add    $0x1,%ebp
mov    %r13d,%r15d
cmp    0xc(%rsp),%ebp
jl     11d0 <main+0x80>
sub    %ebx,%esi
sub    %eax,%esi
add    %ebp,%esi
jmp    124e <main+0xfe>
xor    %esi,%esi
lea    0xdc0(%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 = [8 x i8] c"rest.in\00"
@.str.1 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.2 = [9 x i8] c"rest.out\00"
@.str.3 = [2 x i8] c"w\00"
@stdout = global i32 0
@.str.4 = [3 x i8] c"%i\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* @stdin
%13 = call i32 @freopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = load i32, i32* @stdout
%15 = call i32 @freopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %14)
store i32 -1, i32* %9
store i32 0, i32* %10
store i32 1, i32* %11
%16 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %8)
store i32 1, i32* %6
br label %17
17: ; preds = %48, %2
%18 = load i32, i32* %6
%19 = load i32, i32* %8
%20 = icmp sle i32 %18, %19
br i1 %20, label %21, label %51
21: ; preds = %17
%22 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %7)
%23 = load i32, i32* %7
%24 = load i32, i32* %9
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %37
26: ; preds = %21
%27 = load i32, i32* %11
%28 = add nsw i32 %27, 1
store i32 %28, i32* %11
%29 = load i32, i32* %11
%30 = icmp sgt i32 %29, 2
br i1 %30, label %31, label %36
31: ; preds = %26
%32 = load i32, i32* %11
%33 = sub nsw i32 %32, 1
%34 = load i32, i32* %10
%35 = sub nsw i32 %34, %33
store i32 %35, i32* %10
br label %36
36: ; preds = %31, %26
br label %38
37: ; preds = %21
store i32 1, i32* %11
br label %38
38: ; preds = %37, %36
%39 = load i32, i32* %7
%40 = call i32 @add(i32 %39, i32 1)
%41 = load i32, i32* %6
%42 = load i32, i32* %7
%43 = call i32 @sum(i32 %42)
%44 = sub nsw i32 %41, %43
%45 = load i32, i32* %10
%46 = add nsw i32 %45, %44
store i32 %46, i32* %10
%47 = load i32, i32* %7
store i32 %47, i32* %9
br label %48
48: ; preds = %38
%49 = load i32, i32* %6
%50 = add nsw i32 %49, 1
store i32 %50, i32* %6
br label %17
51: ; preds = %17
%52 = load i32, i32* %10
%53 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %52)
ret i32 0
}

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