count the number of inversions