improved pre-calculating the factorial