from big x^y modulo function by Michael Neve
provide the solution when the normal mod function can't because the input is to big

mmod(x,y,m)
function res = mmod(x,y,m)
% Modulo function : computes mod(x^y,m) for x and y big

% Done by Michael Neve on Matlab 6 (10-09-2001)

if ( y == 1)
    res = mod(x,m);
else
    y1 = y/2;
    if (ceil(y1) == y/2)
        res = mod((mmod(x,y1,m)).^2,m);
    else
        res = mod(mmod(x,floor(y1),m).*mmod(x,floor(y1)+1,m),m);
    end
end

Contact us at files@mathworks.com