Problem Statement
For two sets of integers, A and B, such that AcapB=emptyset, we define f(A,B) as follows.
- Let C=(C1,C2,dots,C∣A∣+∣B∣) be a sequence consisting of the elements of AcupB, sorted in ascending order.
- Take k1,k2,dots,k∣A∣ such that $A=\\lbrace C_{k_1},C_{k_2},\\dots,C_{k_{|A|}}\\rbrace$. Then, let displaystylef(A,B)=sumi=1∣A∣ki.
For example, if A=lbrace1,3rbrace and B=lbrace2,8rbrace, then C=(1,2,3,8), so A=lbraceC1,C3rbrace; thus, f(A,B)=1+3=4.
We have N sets of integers, S1,S2dots,SN, each of which has M elements. For each i(1leqileqN), $S_i = \\lbrace A_{i,1},A_{i,2},\\dots,A_{i,M}\\rbrace$. Here, it is guaranteed that SicapSj=emptyset(ineqj).
Find $\\displaystyle \\sum_{1\\leq i<j \\leq N} f(S_i, S_j)$.
Constraints
- 1leqNleq104
- 1leqMleq102
- 1leqAi,jleq109
- If i1neqi2 or j1neqj2, then Ai1,j1neqAi2,j2.
- All input values are integers.
The input is given from Standard Input in the following format:
N M
A1,1 A1,2 dots A1,M
vdots
AN,1 AN,2 dots AN,M
Output
Print the answer as an integer.
3 2
1 3
2 8
4 6
Sample Output 1
12
S1 and S2 respectively coincide with A and B exemplified in the problem statement, and f(S1,S2)=1+3=4. Since f(S1,S3)=1+2=3 and f(S2,S3)=1+4=5, the answer is 4+3+5=12.
1 1
306
Sample Output 2
0
4 4
155374934 164163676 576823355 954291757
797829355 404011431 353195922 138996221
191890310 782177068 818008580 384836991
160449218 545531545 840594328 501899080
Sample Output 3
102