PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000212. Chụp ảnh các ngôi sao (Starry Landscape Photo)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Chụp ảnh các ngôi sao (Starry Landscape Photo)

Starry Landscape Photo

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

On planet AtCoder, NN stars are arranged in a line from east to west. Their positions are numbered 11 through NN, starting with the easternmost star.

All stars have distinct brightness ranks. The star at position ii has rank BiB_i: rank 11 is the brightest. The sequence BB is a permutation of 1,2,…,N1,2,\ldots,N.

Takahashi takes a photograph using the following two steps:

  1. Choose l,rl,r with 1≤l≤r≤N1\le l\le r\le N. Only stars at consecutive positions ll through rr fit into the frame.
  2. Choose bb with 1≤b≤N1\le b\le N. Among the framed stars, capture exactly those with brightness rank at most bb, namely Bi≤bB_i\le b.

The photograph must not be empty. Two photographs are considered identical if they show the same set of stars, even if their choices of l,r,bl,r,b differ.

Count the distinct nonempty sets of stars that can appear in a photograph.

Input

  • The first line contains the integer NN, the number of stars.
  • The second line contains exactly NN integers B1,B2,…,BNB_1,B_2,\ldots,B_N, in east-to-west order. They form a permutation of 11 through NN.

Output

Print one integer: the number of distinct nonempty sets of stars that can be photographed. Do not reduce the answer modulo any number.

Subtasks

  • Subtask 1 (20 points): 1≤N≤81\le N\le 8; 1≤Bi≤N1\le B_i\le N for each 1≤i≤N1\le i\le N; Bi≠BjB_i\ne B_j for all 1≤i<j≤N1\le i<j\le N.
  • Subtask 2 (30 points): 1≤N≤40001\le N\le 4000; 1≤Bi≤N1\le B_i\le N for each 1≤i≤N1\le i\le N; Bi≠BjB_i\ne B_j for all 1≤i<j≤N1\le i<j\le N.
  • Subtask 3 (50 points): 1≤N≤5000001\le N\le 500000; 1≤Bi≤N1\le B_i\le N for each 1≤i≤N1\le i\le N; Bi≠BjB_i\ne B_j for all 1≤i<j≤N1\le i<j\le N.

Examples

Example 1

Input

4
3 1 4 2

Output

12

Explanation

The brightness ranks from east to west are 3,1,4,23,1,4,2. Group photographed sets by their largest rank: rank 11 contributes 11 set; rank 22 contributes 22 sets; rank 33 contributes 33 sets; and rank 44 contributes (2+1)(1+1)=6(2+1)(1+1)=6 sets. In total, 1+2+3+6=121+2+3+6=12 distinct nonempty sets can be photographed. For example, l=2,r=4,b=3l=2,r=4,b=3 captures the stars of ranks 11 and 22.

Example 2

Input

7
1 2 3 4 5 6 7

Output

28

Explanation

Ranks increase from 11 to 77 in east-to-west order. For each possible largest rank xx, there are exactly xx obtainable sets whose largest rank is xx, corresponding to consecutive segments ending at position xx. Hence the answer is 1+2+3+4+5+6+7=281+2+3+4+5+6+7=28.