The result of $num % 1000000007
will always be $num
for all values less than 1000000007. So if all values in @array
are within the range 0 .. 50,000, such a calculation is redundant. You would have to do two steps, and not use the *=
operator:
$ans = ($ans % 1000000007) * $_ for @array;
Word of caution, though. For any non-prime modulo there's always the risk that your modulo operation results in zero, which will of course cause the entire multiplication to produce zero. I think you've already thought of this, since 1000000007 seems to be a prime number, but I'll just mention it anyway.
ETA: Reusing intermediate products:
for (@array) {
$ans *= $_;
print "Before mod: $ans\n";
$ans %= 1000000007;
print "After mod : $ans\n";
}
Note that you do not need to compound the operators here.