Your approach to the problem is correct. But the problem with your solution is that if you have the number of the form a / b and if a is growing very big you are making it a = a%mod i.e. you are taking the modulus of the value and in the similar case if the value of denominator b is large you are taking the modulus of b also. But this makes the actual value of a divided by b incorrect.
This is because
a / b
may not be equal to
a%mod / b%mod.
Hence in that case instead of dividing a%modulus by b%modulus multiply the multiplicative inverse of b%modulus with respect to modulus to a%modulus and then again take the modulus to get the result. The multiplicative inverse can be found using Fermat Test available on this link http://comeoncodeon.wordpress.com/.