SIGFPE error

I am trying to solve marbles problem (http://www.codechef.com/problems/MARBLES) and getting SIGFPE error. It is written that this error comes due to division by 0 but i am unable to figure out why it is happening in my code.

#include<stdio.h>

unsigned long comb(unsigned long n, unsigned long r)
{
     if(n==r || r==0)
     return (unsigned long)1;
     if(r==1)
     return n;

     unsigned long nn=1,nr=1;
    while(r)
    {
            nn=nn*(n--);
            nr=nr*(r--);
            }
             
      return (nn/nr);
}

int main()
{
int t;
unsigned long n,r;
scanf("%d",&t);
while(t--){
scanf("%lu%lu",&n,&r);
printf("%lu\n",comb(--n,--r));
}
return 0;
}

Here is a possible reason. You are trying to calculate n * (n-1) * (n-2) * … * (r+1)/ (n-r)!. Now the range of numbers n and r is very large so the factorials will not fit in the range of unsigned long so it will round up after it reaches its limit. It might be possible that the value of nr becomes 0 because of this causing SIGFPE.

//