/* Question: Given the sequence, one of the subsequences is (x, y, z). Ask how many of these subsequences are there at most, so that x <z <y, in this analysis, the sequence is 1 to n: because it is an arrangement, so there is no same element, so x, y, z are not equal to each other. Therefore, the size relationship in the sequence can be expressed as small, medium, large, and small, and = small ____, so the answer is small, medium, and small _-small, medium, and small _. You can count the number of all elements after this bit as X, then the small _ = x * (x-1)/2 and small statistics in the big, just need to count the first and later than his big number x, y, the number of small-to-large = x * y statistics can be calculated using a tree array. The tree array can calculate the number of previously added elements smaller than the current element. */ # Include <Iostream> # Include <Cstdio> # Include <Cstring> Using Namespace STD; # Define Mod4 100000007 # Define X 100005 Int A [x]; Long Long C [X], big [X], SMA [x]; Int Lowbit ( Int X ){ Return X &- X ;} Void Modify ( Int X ){ While (X <X) {C [x] = (C [x] + 1 ) % MOD; x + = Lowbit (x );}} Long Long Query ( Int X ){ Long Long Ret = 0 ; While (X> 0 ) {RET + = C [X]; RET % = MOD; x -= Lowbit (x );} Return RET ;} Int Main () {freopen ( " Sum. In " , " R " , Stdin); freopen ( " Sum. Out " , " W " , Stdout ); Int CNT = 0 , T, n; CIN > T; While (T -- ) {Printf ( " Case # % d: " , ++CNT); scanf ( " % D " ,& N); memset (C, 0 , Sizeof (C); memset (big, 0 , Sizeof (BIG); memset (SMA, 0 , Sizeof (SMA )); For (Int I = 1 ; I <= N; I ++ ) {Scanf ( " % D " ,& A [I]); SMA [I] = Query (A [I]); /// Count the number smaller than him Modify (A [I]);} memset (C, 0 , Sizeof (C )); For (Int I = N; I> = 1 ; I --) /// Count the number later than him {Big [I] = N-I- Query (A [I]); Modify (A [I]);} Long Long S = 0 , B = 0 ; For ( Int I = 1 ; I <= N; I ++ ) {S = (S + (BIG [I]- 1 ) * Big [I]/ 2 ) % MOD; /// Count the number of small _ B = (B + big [I] * SMA [I]) % MOD; /// Count the numbers in a Small and Medium Size } Cout <(S-B + mod) % mod < Endl ;} Return 0 ;}