1. 


/**
* ACM programming Contest WS 08/09
10810 Ultra Quick Sort
* UVa Status: AC
* Run Time: 0.340
* Programming Language: ANSI C
* Date: 16.06.2009
* @author Doina Logofatu logofatu@hm.edu
*/

#include <stdlib.h>

#define N 500000

long long s;
long long *a;

void merge(long left, long mid, long right)
{
long i, left_end, num_elements;

long long *temp = (long long *) malloc((right - left + 1) *sizeof(long long ));
long tmpInx = 0;
long l=left, r=right;

left_end = mid - 1;
num_elements = right - left + 1;

while ((left <= left_end) && (mid <= right))
{
if (a[left] <= a[mid])
{

temp[tmpInx++]=a[left++];

}
else /* numbers[left]>numbers[mid] */
{
temp[tmpInx++]=a[mid++];

s+=left_end-left+1;
}
}


while(left<=left_end) { temp[tmpInx++]=a[left++]; }

while(mid<=right) temp[tmpInx++]=a[mid++];


memcpy (a+l, temp, num_elements *sizeof(long long ));

/*for(i=l; i<=r; i++) a[i]=temp[i-l];*/


}

/*mergesort*/
void m_sort(long left, long right)
{
long mid;

if (right > left)
{
mid = (right + left) / 2;
m_sort(left, mid);
m_sort(mid+1, right);

merge(left, mid+1, right);
}
}


int main(){


long n, i;

a = (long long *) malloc(N*sizeof(long long ));

while(scanf("%ld", &n)==1 && n!=0){

s=0;
for(i=0; i<n; i++){
scanf("%lld", a+i);
}

m_sort(0, n-1);

printf("%lld\n", s);

}

return 0;
}