sub f{map{push@{-$_},"$a[--$|] $'"for@a=/\d+ /g}<>;my$r=1;$r+=$t=!$c[$_]++&&&f,$x+=/ /*$'*$t*($N-$t)for@{-$_};$r}f for$N=<>;print$x*2