Added flag to prevent wraparound.
Added helper function prod.