Optimized factorial