module mdl integer*8::f(10000000) contains recursive function fib(n,m)result(res) integer*8::n,m,res if(f(n)/=-1)then res=f(n) else res=mod(fib(n-1,m)+fib(n-2,m),m) f(n)=res end if end function end module program main use mdl implicit none integer*8::i,j,k,n,m read(*,*)n,m f(1)=0 f(2)=mod(1,m) do i=3,size(f) f(i)=-1 end do print*,fib(n,m) end program