2008F.md (2150B)
1 Source: https://codeforces.com/problemset/problem/2008/F 2 3 F. Sakurako's Box 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Sakurako has a box with $n$ balls. Each ball has it's value. She wants to bet with her friend that if the friend randomly picks two balls from the box (it could be two distinct balls, but they may have the same value), the product of their values will be the same as the number that Sakurako guessed. 22 23 Since Sakurako has a PhD in probability, she knows that the best number to pick is [the expected value](http://tiny.cc/matozh_en), but she forgot how to calculate it. Help Sakurako and find the expected value of the product of two elements from the array. 24 25 It can be shown that the expected value has the form $\frac{P}{Q}$, where $P$ and $Q$ are non-negative integers, and $Q \ne 0$. Report the value of $P \cdot Q^{-1}(\bmod 10^9+7)$. 26 27 Input 28 29 The first line contains a single integer $t$ ($1\le t\le 10^4$) — the number of test cases. 30 31 The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$) — the number of elements in the array. 32 33 The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0\le a_i\le 10^9$) — the elements of the array. 34 35 It is guaranteed that the sum of $n$ across all test cases does not exceed $2\cdot 10^5$. 36 37 Output 38 39 For each test case, output the value of $P \cdot Q^{-1}(\bmod 10^9+7)$. 40 41 Example 42 43 Input 44 45 ``` 46 3 47 48 3 49 50 3 2 3 51 52 4 53 54 2 2 2 4 55 56 5 57 58 1 2 3 4 5 59 ``` 60 61 Output 62 63 ``` 64 7 65 6 66 500000012 67 ``` 68 69 Note 70 71 For the first test, Sakurako's friend can pick these pairs of balls: $(a_1,a_2)$, $(a_1,a_3)$, $(a_2,a_3)$. Their products equal to $3\cdot 2=6$ , $3\cdot 3=9$ , $3\cdot 2=6$ respectively, so the expected value is $\frac{6+9+6}{3}=7$. 72 73 For the second test, Sakurako's friend can pick these pairs of balls: $(a_1,a_2)$, $(a_1,a_3)$, $(a_1,a_4)$, $(a_2,a_3)$, $(a_2,a_4)$, $(a_3,a_4)$. Their products equal to $2\cdot 2=4$ , $2\cdot 2=4$ , $2\cdot 4=8$, $2\cdot 2=4$, $2\cdot 4=8$, $2\cdot 4=8$ respectively, so the expected value is $\frac{4+4+8+4+8+8}{6}=\frac{36}{6}=6$.