static long mod(long dividend, long divisor) {
        if (divisor < 0) {
            throw new IllegalArgumentException("divisor must be positive: " + divisor);
        }
        if (dividend >= 0) {
            return dividend % divisor;
        } else {
            return (dividend + 1) % divisor - 1 + divisor;
        }
    }