#P8850. [JRKSJ R5] Jalapeno and Garlic

[JRKSJ R5] Jalapeno and Garlic

Background

Problem Description

There is a cycle with nn vertices. Each vertex has a weight aa, and the vertices are numbered from 11 to nn. Vertex 11 is adjacent to vertex nn.

You want there to be exactly one x[1,n]x\in[1,n] such that ax0a_x\ne 0. To achieve this, you need to perform operations according to the following process:

  1. Choose an xx, meaning that in the end you want ax0a_x\ne 0. After that, you cannot change your choice of xx.
  2. Perform some number of modification operations. In each operation, you may choose a y[1,n]y\in[1,n] and set ayay1a_y\gets a_y-1. At the same time, among the two vertices adjacent to yy, choose one uniformly at random, and its weight will be increased by 11.

You want the expected number of modification operations to be as small as possible. Therefore, compute the expected number of operations under the optimal strategy (operation 1 is not counted).

Input Format

The first line contains an integer nn.
The second line contains nn integers representing a1na_{1\dots n}.

Output Format

Output an integer representing the answer. Print the answer modulo 10045358091004535809.

2
114514 1919810
114514
3
1 1 2
4

Hint

Explanation for Sample 11

Choose x=2x=2, and perform 114514114514 operations, each time taking y=1y=1.

Constraints

This problem uses bundled testdata.

Subtask\text{Subtask} nn\le Score
11 22 55
22 10310^3 2020
33 10410^4
44 10510^5
55 10610^6 3535

For 100%100\% of the testdata, 2n1062\le n\le 10^6 and 0ai<10045358090\le a_i<1004535809.

Translated by ChatGPT 5