gets.to_i.times{ n=gets.to_i a=gets.split.map(&:to_i) f={a.sort=>1} (n-1).times{ s=Hash.new{0} f.each{|k,v| [*0..k.size-1].combination(2){|i,j| kd = k.dup jj = kd.delete_at(j) ii = kd.delete_at(i) kd << (ii+jj)+(ii*jj) s[kd.sort] += v } } f=s } p f.map{|k,v|k[0]*v}.inject(:*)/f.values.inject(:+) }