If the module M sufficiently smaller than ULLONG_MAX (which takes place if it is in the region of 10 ^ 10), you can do this in three steps by dividing one of the factors into two parts. I assume that A < M and B < M and M < 2^42 .
For larger values, you can divide the coefficient into three parts, but if the module becomes really close to ULLONG_MAX , it becomes ugly.
Daniel Fischer
source share