Editorial
The angular order of and swaps when the line through them intersects . If the two lines are not parallel, this happens exactly once and swaps the two indices in the permutation, contributing . If their line is parallel to , no such change occurs.
The sum of over all pairs is
For , set . The line is parallel to exactly when . Sort points by , compute the sum of inside every equal-value group, and subtract it from the total. The time complexity is .