Problem F

Statement
Copy Copied
Description:
Programming teacher Dmitry Olegovich is going to propose the following task for one of his tests for students:

You are given a tree T with n vertices, specified by its adjacency matrix a[1... n, 1... n]. What is the output of the following pseudocode?

In order to simplify the test results checking procedure, Dmitry Olegovich decided to create a tree T such that the result is his favorite sequence b. On the other hand, Dmitry Olegovich doesn't want to provide students with same trees as input, otherwise they might cheat. That's why Dmitry Olegovich is trying to find out the number of different trees T such that the result of running the above pseudocode with T as input is exactly the sequence b. Can you help him?

Two trees with n vertices are called different if their adjacency matrices a1 and a2 are different, i. e. there exists a pair (i, j), such that 1 ≤ i, j ≤ n and a1[i][j] ≠ a2[i][j].

Input Format:
The first line contains the positive integer n (1 ≤ n ≤ 500) — the length of sequence b.

The second line contains n positive integers b1, b2, ..., bn (1 ≤ bi ≤ n). It is guaranteed that b is a permutation, or in other words, each of the numbers 1, 2, ..., n appears exactly once in the sequence b. Also it is guaranteed that b1 = 1.

Output Format:
Output the number of trees satisfying the conditions above modulo 109 + 7.

Note:
None

Submissions

IDLanguageExit CodeTimestampCodeStdoutStderrRetry
4661 go 0 2026-02-28T09:40:33.983612Z View View View Retry
4660 cpp 0 2026-02-28T09:40:17.852761Z View View View Retry
4659 cpp 1 2026-02-28T09:33:40.268171Z View View View Retry
4658 go 1 2026-02-28T09:33:31.533543Z View View View Retry
4657 cpp 1 2026-02-28T09:30:00.226031Z View View View Retry

Evaluations

Eval IDRun IDProviderModelLangSuccessTimestampPromptResponseStdoutStderrRetry
1165 20260227-135649 openrouter x-ai/grok-4 go false 2026-02-27T18:13:30.145159Z View View View View Retry