-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSakurakoBox.java
More file actions
52 lines (43 loc) · 1.41 KB
/
SakurakoBox.java
File metadata and controls
52 lines (43 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package Divisions.Div3.Round970;
//code by senurah
import java.util.Scanner;
public class SakurakoBox {
static final long MOD = 1000000007;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
if (t >= 1 && t <= 10000) {
for (int i = 0; i < t; i++) {
int n = scanner.nextInt();
long[] a = new long[n];
long sumA = 0;
long sumA2 = 0;
for (int j = 0; j < n; j++) {
a[j] = scanner.nextLong();
sumA = (sumA + a[j]) % MOD;
sumA2 = (sumA2 + a[j] * a[j]) % MOD;
}
long P = (sumA * sumA - sumA2 + MOD) % MOD;
P = (P * modInverse(2, MOD)) % MOD;
long Q = n * (n - 1) / 2;
long Q_inv = modInverse(Q, MOD);
long result = (P * Q_inv) % MOD;
System.out.println(result);
}
}
}
static long modInverse(long a, long mod) {
return power(a, mod - 2, mod);
}
static long power(long base, long exp, long mod) {
long result = 1;
while (exp > 0) {
if ((exp & 1) != 0) {
result = (result * base) % mod;
}
base = (base * base) % mod;
exp >>= 1;
}
return result;
}
}