Write a Fortran program to calculate the factorial function. Be sure to handle the special cases of 0! and of illegal input values.