I got this code written in JavaScript, but it returns wrong number for large input.
 
It should calculate the base to the exponent(ex) power with modulo(mo). 
I wrote equivalent code in C and is working. Please can someone tell me what is wrong. 
Try to test Fermat's theorem for modulo 10^9 +7.
function powFun(base, ex, mo) {
    var r;
    if(ex === 0) 
        return 1;
    else if(ex % 2 === 0) {
        r = powFun(base, ex/2, mo) % mo ;
        return (r * r) % mo;
    }else 
        return (base * powFun(base, ex - 1, mo)) % mo;
}
 
     
    