$n=<>; for(glob<>){ $a+=$_==1; $b+=$_==2; $c+=1; } $n==$c or die; print $a*$b*2+$a*($a-1)/2+$n*($n-1)/2;