Modular Arithmetic That Doesn't Overflow
In short: a % m is the remainder after dividing a by m, and it always
lands in [0, m) in Python — negatives wrap around to the top of the
range instead of going below zero. Other languages (C, Java, JavaScript)
truncate toward zero instead, so a negative a gives a negative remainder
there. For a running total, mod after every add or multiply, not once at
the end — the in-between value can overflow in other languages, and even in
Python it just gets slower to work with as it grows into a giant integer.

Spreading load across shards
Quarry assigns a record to one of shard_count shards with
record_id % shard_count. Record ids are usually positive, but a signed hash
or a delta between two ids can go negative — and that's where languages
disagree.
In Python this is always a safe shard index. Port the same formula to C,
Java or JavaScript and -3 % 8 comes back -3 — a negative array index,
which crashes or silently reads the wrong shard. Rule: if a value can go
negative, normalize it yourself: ((x % m) + m) % m works the same in every
language, because it adds one more m before the final mod, and a value
already in range is unaffected.
def safe_mod(x, m):
return ((x % m) + m) % m
print(safe_mod(-3, 8), safe_mod(11, 8), safe_mod(-16, 8))