#P58. [KBC005B] Sheep

[KBC005B] Sheep

Source

This problem is adapted from Long Long OJ. All rights reserved.

Problem Description

Little A built a circular sheepfold with nn rooms, numbered clockwise from 11 to nn. The room numbered nn is adjacent to the room numbered 11, and adjacent rooms are connected by passages. Each room has a door opening outward.

Little A wants to raise sheep in these rooms, with rir_i sheep in the ii-th room. To drive the sheep into the rooms in an orderly manner, he plans to open the outward door of one room so that all sheep enter through this door.

After entering the room, the sheep walk clockwise through the rooms until they reach their own room. Little A wants to minimize the total distance traveled by all sheep. Please help Little A determine which room's door to open for all sheep to enter, such that the total distance traveled by the sheep is minimized, and find this minimum total distance. The distance traveled by each sheep is the number of rooms it passes through.

Input Format

The first line contains only one integer nn.

The next line contains rir_i (separated by spaces), representing the number of sheep in the ii-th room.

Output Format

Output only one number, which is the minimum value of the total distance traveled by the sheep.

The answer should be taken modulo 998244353998244353.

Samples

5
4 7 8 6 4
48

Sample 1 Explanation

There are 55 rooms in total: 44 sheep in Room 11, 77 sheep in Room 22, 88 sheep in Room 33, 66 sheep in Room 44, and 44 sheep in Room 55.

The optimal way is to open the door of Room 22 and let all sheep enter through Room 22. In this case:

  • 77 sheep (in Room 22) do not need to walk;
  • 88 sheep walk to Room 33, with a total distance of 1×8=81 \times 8 = 8;
  • 66 sheep walk to Room 44, with a total distance of 2×6=122 \times 6 = 12;
  • 44 sheep walk to Room 55, with a total distance of 3×4=123 \times 4 = 12;
  • 44 sheep walk to Room 11, with a total distance of 4×4=164 \times 4 = 16.

The total distance is 8+12+12+16=488 + 12 + 12 + 16 = 48.

Data Range

  • For 10%10\% of the data: 1n10,1ri101 \le n \le 10, 1 \le r_i \le 10;
  • For 20%20\% of the data: 1n102,1ri1021 \le n \le 10^2, 1 \le r_i \le 10^2;
  • For 30%30\% of the data: 1n103,1ri1021 \le n \le 10^3, 1 \le r_i \le 10^2;
  • For 40%40\% of the data: 1n105,1ri1031 \le n \le 10^5, 1 \le r_i \le 10^3;
  • For 50%50\% of the data: 1n106,1ri1051 \le n \le 10^6, 1 \le r_i \le 10^5;
  • For 60%60\% of the data: 1n2×106,1ri2×1051 \le n \le 2\times10^6, 1 \le r_i \le 2\times10^5;
  • For 80%80\% of the data: 1n3×106,1ri2×1051 \le n \le 3\times10^6, 1 \le r_i \le 2\times10^5;
  • For 100%100\% of the data: 1n5×106,1ri2×1051 \le n \le 5\times10^6, 1 \le r_i \le 2\times10^5.