Under construction

Still being built. Things may change

Building 3

Time limit1sMemory limit512 MB

Summary
Given the sequence B with one entry deleted from A, where A_i is the longest increasing chain ending at building i, count how many full sequences A could produce B.
Level

Hard8 of 10

Topics
Dynamic programming, Array, Greedy
Solved
No attempts yet

Problem

The International Olympiad in Informatics will be held in Japan. To welcome the contestants, the city plans to decorate the tall buildings along the main avenue from the airport to the hotel where the athletes stay. A well-known designer said the buildings used for decoration must get taller as you move from the airport toward the hotel. In other words, if the heights of the decorated buildings are h1,h2,h3,⋯h_1, h_2, h_3, \cdots in order from the one closest to the airport, then h1<h2<h3<⋯h_1 < h_2 < h_3 < \cdots must hold.

To make the decoration as impressive as possible, the number of decorated buildings should be as large as possible. JOI was asked to choose the buildings. He realized that a building owner might demand two things at once: that the owner's building must be used for decoration, and that it must be the one closest to the hotel among the decorated buildings, so that it stands out.

There are NN buildings along the avenue. Call the building that is ii-th closest to the airport building ii (1≤i≤N1 \le i \le N). All NN heights are different. To be ready for any such demand, JOI computed the following in advance. Suppose building ii is used for decoration and is the closest to the hotel among the decorated buildings. Then the maximum number of buildings that can be chosen is AiA_i. JOI wrote the integer sequence A1,A2,…,ANA_1, A_2, \ldots, A_N and submitted it to Chairman K of the JOI committee.

However, the note Chairman K received contained only the integer sequence B1,B2,…,BN−1B_1, B_2, \ldots, B_{N-1} of length N−1N-1. Chairman K does not know the building heights, so Chairman K cannot compute the values AiA_i.

Chairman K thinks JOI forgot one number. Among all possible integer sequences A1,A2,…,ANA_1, A_2, \ldots, A_N, how many become B1,B2,…,BN−1B_1, B_2, \ldots, B_{N-1} after deleting exactly one entry?

JOI may have made other mistakes as well. For some values of BB, there may be no such sequence at all.

Write a program that counts the possible sequences A1,…,ANA_1, \ldots, A_N that become B1,…,BN−1B_1, \ldots, B_{N-1} after removing one entry.

Input

The first line contains NN, the number of buildings along the avenue. Each of the next N−1N-1 lines contains BjB_j (1≤j≤N−11 \le j \le N-1), the jj-th value in the note.

Output

Print one integer on a single line: the number of possible sequences A1,…,ANA_1, \ldots, A_N that become B1,…,BN−1B_1, \ldots, B_{N-1} after removing one entry.

Constraints

2≤N≤1 000 0002 \le N \le 1\,000\,000.

1≤Bj≤N1 \le B_j \le N for 1≤j≤N−11 \le j \le N-1.

Examples2

  1. Example 1

    Input
    4
    1
    1
    2
    
    Expected output
    5
    
  2. Example 2

    Input
    8
    1
    1
    2
    1
    2
    3
    1
    
    Expected output
    15