Corridor

시간 제한1초메모리 제한2048 MB

요약
데이비드가 1번 칸에서 N번 칸으로 걸어간다. 칸에 들어가면 텔레포터가 켜지거나 꺼지고, 켜져 있으면 더 뒤쪽 목표 칸으로 순간 이동한다. 출구까지 걸은 총 시간을 10^9+7로 나눈 나머지를 구한다.
난이도

보통10점 중 7점

유형
동적 계획법, 시뮬레이션, 위상 정렬
정답자
아직 제출이 없습니다

문제

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 (1≤N≤100,0001 \le N \le 100\\,000), the number of segments the corridor consists of.

The second line contains NN integers A_1,A_2,…,A_NA\_1, A\_2, \ldots, A\_N (1≤A_i≤i1 \le A\_i \le i) denoting the teleporter targets. If A_i=iA\_i = i, then the ii-th segment is empty. Otherwise the ii-th segment contains a teleporter which teleports stuff into the A_iA\_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.

예제3

  1. 예제 1

    입력
    5
    1 2 2 1 5
    
    예상 출력
    10
    
  2. 예제 2

    입력
    3
    1 1 2
    
    예상 출력
    6
    
  3. 예제 3

    입력
    5
    1 1 1 1 1
    
    예상 출력
    31