program main implicit none integer :: n integer :: m integer :: i, j double precision :: p double precision :: expected_trees read *, n, m read *, p if (n == 1) then expected_trees = merge(p, dble(m-2) * p**3 + 2.0d0 * p**2, m == 1) else expected_trees = merge( & dble(n-2) * p**3 + 2.0d0 * p**2, & dble(n-2) * dble(m-2) * p**5 + dble(2*n + 2*m - 8) * p**4 + 4.0d0 * p**3, & m == 1 & ) end if print *, expected_trees end program main