To prevent overcounting
pairsum -= prev ; prev = temp ; let secondway = prefix [ size1 - 1 ] ; if ( j != 0 ) secondway -= prefix [ j - 1 ] ; let answer = count * ( count - 1 ) * secondway * ( secondway - 1 ) ; answer = Math . floor ( answer / 4 ) ; answer += Math . floor ( ( pairsum * secondway * ( secondway - 1 ) ) / 2 ) ;