```ts
const MODULUS = 1000000007;

/**
 * The nth Fibonacci number, modulo 1000000007.
 */
export function fibMod(n: number): number {
    if (typeof n !== 'number' || n < 0) {
        throw new Error("n must be a non-negative integer.");
    }
    
    const MOD = BigInt(MODULUS);
    let a = BigInt(0), b = BigInt(1);

    for (let i = 2; i <= n; i++) {
        const next = (a + b) % MOD;
        a = b;
        b = next;
    }

    return a.valueOf();
}
```