To see how this works we note first that (aNi−1)MOD(M)=(aNi−1−[Ni−1/q]M)MOD(M), since we can add or subtract any integer multiple of M from aNi−1. The last term [Ni−1/q]MMOD(M) is zero since the integer division [Ni−1/q] just yields a constant which is multiplied with M.