private static long FastModInverse(
long v,
long m)
{
if (m < 1)
throw new ArithmeticException("Modulus must be positive");
long[] x = new long[2];
long gcd = FastExtEuclid(v, m, x);
if (gcd != 1)
throw new ArithmeticException("Numbers not relatively prime.");
if (x[0] < 0)
{
x[0] += m;
}
return x[0];
}