Corridor | 프로그래밍의 벗 PivotOJ
PivotOJ

Corridor

시간 제한: 1000ms메모리 제한: 2048MB출처: EIO 2023-24 sel1BOJ 32975

문제

David wants to exit a secret science laboratory. He is in a corridor on the opposite end from the exit, so he will have to walk along the entire length of the corridor.

It would be straightforward to leave, but some teleporters are being tested in the corridor. The corridor consists of NN segments. Some segments contain a teleporter.

Each teleporter is configured to teleport stuff into a particular target segment. The target segment is always further from the exit (and thus closer to David's starting position) than the teleporter itself. Also each teleporter may be switched on or off.

When David enters (either walks or is teleported into) a segment with a turned-on teleporter, he is teleported into the target segment, and the teleporter turns off. When David enters a segment with a turned-off teleporter, he is not teleported, but the teleporter turns on.

All teleporters are turned on initially.

Find how long it will take David to leave the laboratory.

It takes one second for David to walk from one segment to the next. Teleportation is instantaneous. David will always walk towards the exit. Exiting the laboratory from the last segment of the corridor also takes one second.

Since the answer may be very large, output the remainder when David's total walking time (in seconds) is divided by 109+710^9 + 7.

입력

The first line contains one integer NN (1N1000001 \le N \le 100\,000), the number of segments the corridor consists of.

The second line contains NN integers A1,A2,,ANA_1, A_2, \ldots, A_N (1Aii1 \le A_i \le i) denoting the teleporter targets. If Ai=iA_i = i, then the ii-th segment is empty. Otherwise the ii-th segment contains a teleporter which teleports stuff into the AiA_i-th segment.

David starts at the 11-st segment.

출력

Output one integer, David's time to exit the lab, modulo 109+710^9 + 7.

예제

예제 1

입력
5
1 2 2 1 5
출력
10

예제 2

입력
3
1 1 2
출력
6

예제 3

입력
5
1 1 1 1 1
출력
31
코드를 제출하려면 로그인하세요.