Divide by Zero 2021 and Codeforces Round 714 (Div. 2)
E
Cost Equilibrium
Codeforces
2300
原题
← DF →
翻译中…

An array is called beautiful if all the elements in the array are equal.

翻译中…

You can transform an array using the following steps any number of times:

翻译中…
  1. Choose two indices $i$ and $j$ ($1 \leq i,j \leq n$), and an integer $x$ ($1 \leq x \leq a_i$). Let $i$ be the source index and $j$ be the sink index.
  2. Decrease the $i$-th element by $x$, and increase the $j$-th element by $x$. The resulting values at $i$-th and $j$-th index are $a_i-x$ and $a_j+x$ respectively.
  3. The cost of this operation is $x \cdot |j-i| $.
  4. Now the $i$-th index can no longer be the sink and the $j$-th index can no longer be the source.
翻译中…

The total cost of a transformation is the sum of all the costs in step $3$.

翻译中…

For example, array $[0, 2, 3, 3]$ can be transformed into a beautiful array $[2, 2, 2, 2]$ with total cost $1 \cdot |1-3| + 1 \cdot |1-4| = 5$.

翻译中…

An array is called balanced, if it can be transformed into a beautiful array, and the cost of such transformation is uniquely defined. In other words, the minimum cost of transformation into a beautiful array equals the maximum cost.

翻译中…

You are given an array $a_1, a_2, \ldots, a_n$ of length $n$, consisting of non-negative integers. Your task is to find the number of balanced arrays which are permutations of the given array. Two arrays are considered different, if elements at some position differ. Since the answer can be large, output it modulo $10^9 + 7$.

翻译中…

Input

翻译中…

The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the size of the array.

翻译中…

The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$).

翻译中…

Output

翻译中…

Output a single integer — the number of balanced permutations modulo $10^9+7$.

翻译中…

Note

翻译中…

In the first example, $[1, 2, 3]$ is a valid permutation as we can consider the index with value $3$ as the source and index with value $1$ as the sink. Thus, after conversion we get a beautiful array $[2, 2, 2]$, and the total cost would be $2$. We can show that this is the only transformation of this array that leads to a beautiful array. Similarly, we can check for other permutations too.

翻译中…

In the second example, $[0, 0, 4, 4]$ and $[4, 4, 0, 0]$ are balanced permutations.

翻译中…

In the third example, all permutations are balanced.

翻译中…

样例

Input #1
3
1 2 3
Output #1
6
InputCopy OutputCopy
Input #2
4
0 4 0 4
Output #2
2
InputCopy OutputCopy
Input #3
5
0 11 12 13 14
Output #3
120
InputCopy OutputCopy
题目信息
题号
1513E
时间限制
1 s
内存限制
256 MB
标签
组合数学
构造
数学
排序

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。