#C. Dice Journey

    Type: Default 1000ms 256MiB

Dice Journey

You cannot submit for this problem because the contest is ended. You can click "Open in Problem Set" to view this problem in normal mode.

Dice Journey

Problem Statement

There are NN (2N2×105)(2 \leq N \leq 2 \times 10^5) squares, numbered from 11 through NN. You start on square 11. Each of the squares from 11 through N1N - 1 has a die on it. The die on square ii is labeled with the integers from 00 through AiA_i, each occurring with equal probability. (Die rolls are independent of each other.) Until you reach square NN, you will repeat rolling a die on the square you are on. If the die on square xx rolls the integer yy, you go to square x+yx + y.

Find the expected value, modulo 998244353998244353, of the number of times you roll a die.

It can be proved that the sought expected value is always a rational number. Additionally, if that value is represented PQ\frac{P}{Q} using two coprime integers PP and QQ, there is a unique integer RR such that R×Q=PR \times Q = P (mod998244353)\pmod{998244353} and 0R<9982443530 \leq R < 998244353. Find this RR.

INPUT FORMAT (input arrives from the terminal / stdin):

  • Line 1: A single integer, NN.
  • Line 2: N1N - 1 integers, AiA_i (1AiNi)(1 \leq A_i \leq N - i) for 1iN11 \leq i \leq N - 1, separated by spaces.

OUTPUT FORMAT (print output to the terminal / stdout):

  • Line 1: A single integer, the expected value modulo 998244353998244353 of the number of times you roll a die, represented as described in the problem statement.

SAMPLE INPUT 1:

3
1 1

SAMPLE OUTPUT 1:

4

Let's consider one possible sequence of events until we reach Square NN:

  1. You roll a 11 on Square 11, which allows you to move to Square 22.
  2. On Square 22, you roll a 00, meaning you stay on Square 22.
  3. You roll a 11 on Square 22 next, allowing you to move to Square 33.

This sequence of events occurs with a probability of 14\frac14. It takes 33 die rolls to reach Square NN in this case. However, considering all possible case, the average or expected number of times you roll a die is 44.

SAMPLE INPUT 2:

5
3 1 2 1

SAMPLE OUTPUT 2:

332748122

SCORING:

  • Inputs 3: 1N700001 \leq N \leq 70000
  • Inputs 4-30: No additional constraints.

HFI Coding Club 2023 November Contest

Not Attended
Status
Done
Rule
IOI
Problem
4
Start at
2023-11-16 18:45
End at
2023-11-16 20:45
Duration
2 hour(s)
Host
Partic.
21