Rollout Interleavings

easy combinatorics math number-theory 1000 ms · 256 MB

Rollout Interleavings

A release pushes out k independent services at the same time. Service i has a_i deployment steps (schema migrations, config pushes, health checks, ...) that a deploy bot must run in their fixed order: step 1 before step 2 before step 3, and so on. All services share a single deploy queue, and the bot is free to interleave steps from different services however it likes — the only rule is that each service's own steps must still come out in their original relative order.

Given the step counts a_1, a_2, ..., a_k, count how many distinct sequences the deploy queue could end up executing all a_1 + a_2 + ... + a_k steps in, modulo 1 000 000 007.

Input

The first line contains a single integer k (1 ≤ k ≤ 10^5) — the number of services.

The second line contains k integers a_1, a_2, ..., a_k (1 ≤ a_i), the number of deployment steps for each service. It is guaranteed that a_1 + a_2 + ... + a_k ≤ 10^6.

Output

Print a single integer: the number of distinct valid deploy-queue orders, modulo 1 000 000 007.

Notes

In the first example, service 1 contributes steps X1, X2 and service 2 contributes steps Y1, Y2, Y3. A valid queue order is any arrangement of X1, X2, Y1, Y2, Y3 that keeps X1 before X2 and keeps Y1 before Y2 before Y3; there are C(5, 2) = 10 such arrangements, which matches the first example's answer.

Example 1

Input

2
2 3

Output

10

Example 2

Input

4
1 1 1 1

Output

24

Hints · reveal one at a time if you're stuck

Hint 1

Build the final queue one slot at a time. Once you decide which slots one service occupies, how many ways are there to fill those slots, and what does that leave for the other services?

Hint 2

Each service's steps are forced into one order once its slots are picked, so only the choice of slots matters. Counting these choices uses factorials up to the total number of steps, and division modulo a prime can be done with a modular inverse.

report an issue Prefer your own editor? sooks prep 78 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.