sub MD(){1000000007} $n=<>; $m=1; @a=map$m=$m*$_%MD,glob<>; $s=pop@a; $b=2; $s+=$_*$b%MD,$b=$b*3%MD for reverse@a; print$s;