q = 10^9 comb n r = iterate (scanl1 (\x y -> mod (x+y) q)) [1,1..] !! (n'-r') !! r' where [n',r'] = map fromIntegral [n,r] main = getContents >>= print . otoshidama . map read . words otoshidama :: [Integer] -> Integer otoshidama [n,m] = let r = mod n (1000*m) in mod (comb m (div r 1000)) q