Trying to implement mulmod to avoid overflow