```ts
/*
 * The nth Fibonacci number, modulo 1000000007. fibMod(0) === 0 and fibMod(1) === 1.
 * It must handle n up to 200000 without overflows, with n being a non-negative integer.
 * If n is out of the valid range, it throws an Error.
 *
 * @param n The index in the Fibonacci sequence to compute.
 * @returns The nth Fibonacci number modulo 1000000007.
 */
export function fibMod(n: number): number {
  if (n < 2) {
    return n;
  }

  const result = [0, 1];
  for (let i = 2; i <= n; ++i) {
    const nextFib = (result[i - 1] + result[i - 2]) % MODULUS;
    result.push(nextFib);
  }

  return result[n];
}
```