#ABC470G. mex 之和 / ΣШX

mex 之和 / ΣШX

G - ΣШX

Problem Statement

You are given a length-NN sequence of non-negative integers A=(A1,,AN)A = (A_1, \dots, A_N).

Find the sum of mex(Al,,Ar)\mathrm{mex}({A_l, \cdots, A_r}) over all pairs of integers (l,r)(l, r) satisfying 1lrN1 \leq l \leq r \leq N.

Here, mex(Al,,Ar)\mathrm{mex}({A_l, \cdots, A_r}) denotes the smallest non-negative integer not contained in Al,,ArA_l, \cdots, A_r.

Constraints

  • 1N3×1051 \leq N \leq 3 \times 10^5
  • 0AiN0 \leq A_i \leq N
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

$N$
$A_1$ $A_2$ $\cdots$ $A_N$

Output

Output the answer.


3
1 2 0
5

The answer is 55, the sum of the following 66 values.

  • mex(A1)=mex(1)=0\mathrm{mex}({A_1}) = \mathrm{mex}({1}) = 0
  • mex(A1,A2)=mex(1,2)=0\mathrm{mex}({A_1,A_2}) = \mathrm{mex}({1,2}) = 0
  • $\mathrm{mex}({A_1,A_2,A_3}) = \mathrm{mex}({1,2,0}) = 3$
  • mex(A2)=mex(2)=0\mathrm{mex}({A_2}) = \mathrm{mex}({2}) = 0
  • mex(A2,A3)=mex(2,0)=1\mathrm{mex}({A_2,A_3}) = \mathrm{mex}({2,0}) = 1
  • mex(A3)=mex(0)=1\mathrm{mex}({A_3}) = \mathrm{mex}({0}) = 1

6
2 1 0 2 1 4
31

The answer is 3131, the sum of the following 2121 values.

  • mex(A1)=mex(2)=0\mathrm{mex}({A_1}) = \mathrm{mex}({2}) = 0
  • mex(A1,A2)=mex(2,1)=0\mathrm{mex}({A_1,A_2}) = \mathrm{mex}({2,1}) = 0
  • $\mathrm{mex}({A_1,A_2,A_3}) = \mathrm{mex}({2,1,0}) = 3$
  • $\mathrm{mex}({A_1,A_2,A_3,A_4}) = \mathrm{mex}({2,1,0,2}) = 3$
  • $\mathrm{mex}({A_1,A_2,A_3,A_4,A_5}) = \mathrm{mex}({2,1,0,2,1}) = 3$
  • $\mathrm{mex}({A_1,A_2,A_3,A_4,A_5,A_6}) = \mathrm{mex}({2,1,0,2,1,4}) = 3$
  • mex(A2)=mex(1)=0\mathrm{mex}({A_2}) = \mathrm{mex}({1}) = 0
  • mex(A2,A3)=mex(1,0)=2\mathrm{mex}({A_2,A_3}) = \mathrm{mex}({1,0}) = 2
  • $\mathrm{mex}({A_2,A_3,A_4}) = \mathrm{mex}({1,0,2}) = 3$
  • $\mathrm{mex}({A_2,A_3,A_4,A_5}) = \mathrm{mex}({1,0,2,1}) = 3$
  • $\mathrm{mex}({A_2,A_3,A_4,A_5,A_6}) = \mathrm{mex}({1,0,2,1,4}) = 3$
  • mex(A3)=mex(0)=1\mathrm{mex}({A_3}) = \mathrm{mex}({0}) = 1
  • mex(A3,A4)=mex(0,2)=1\mathrm{mex}({A_3,A_4}) = \mathrm{mex}({0,2}) = 1
  • $\mathrm{mex}({A_3,A_4,A_5}) = \mathrm{mex}({0,2,1}) = 3$
  • $\mathrm{mex}({A_3,A_4,A_5,A_6}) = \mathrm{mex}({0,2,1,4}) = 3$
  • mex(A4)=mex(2)=0\mathrm{mex}({A_4}) = \mathrm{mex}({2}) = 0
  • mex(A4,A5)=mex(2,1)=0\mathrm{mex}({A_4,A_5}) = \mathrm{mex}({2,1}) = 0
  • $\mathrm{mex}({A_4,A_5,A_6}) = \mathrm{mex}({2,1,4}) = 0$
  • mex(A5)=mex(1)=0\mathrm{mex}({A_5}) = \mathrm{mex}({1}) = 0
  • mex(A5,A6)=mex(1,4)=0\mathrm{mex}({A_5,A_6}) = \mathrm{mex}({1,4}) = 0
  • mex(A6)=mex(4)=0\mathrm{mex}({A_6}) = \mathrm{mex}({4}) = 0

25
2 2 1 2 2 0 2 2 2 1 2 2 2 0 2 2 2 2 1 2 2 2 2 0 2
624