Problem:
Score : 400 points

Problem Statement

There is a knight - the chess piece - at the origin (0, 0) of a two-dimensional grid.

When the knight is at the square (i, j), it can be moved to either (i+1,j+2) or (i+2, j+1).

In how many ways can the knight reach the square (X, Y)?

Find the number of ways modulo 10^9 + 7.

Constraints

1 \leq X \leq 10^6

1 \leq Y \leq 10^6

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

Print the number of ways for the knight to reach (X, Y) from (0, 0), modulo 10^9 + 7.

Sample Input 1

3 3

Sample Output 1

2

There are two ways: (0,0) \to (1,2) \to (3,3) and (0,0) \to (2,1) \to (3,3).

Sample Input 2

2 2

Sample Output 2

0

The knight cannot reach (2,2).

Sample Input 3

999999 999999

Sample Output 3

151840682

Print the number of ways modulo 10^9 + 7.

Solution in Haskell (GHC 8.8.3) (judge verdict: Accepted)

{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds           #-}
{-# LANGUAGE KindSignatures      #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE Strict              #-}
{-# LANGUAGE TypeApplications    #-}
import           Data.Proxy   (Proxy (..))
import           Data.Ratio   ((%))
import           GHC.TypeLits (KnownNat, Nat, natVal)

main :: IO ()
main = do
  [x, y] <- map read . words <$> getLine
  print $ solve x y

solve :: Int -> Int -> MInt
solve x y
  | (x + y) `mod` 3 /= 0 = 0
  | otherwise            = comb (n + m) n
  where n = (-x + y * 2) `div` 3
        m = (x * 2 - y) `div` 3

comb :: Int -> Int -> MInt
comb n k
  | n < k || n < 0 || k < 0 = 0
  | otherwise = perm n k `div` product [1 .. k']
  where k' = modulo k :: MInt

perm :: Int -> Int -> MInt
perm _ 0 = 1
perm n k = product [n'-k'+1 .. n']
  where n' = modulo n :: MInt
        k' = modulo k :: MInt

-- Modulo Library
newtype Mod a (n :: Nat) = Mod { unMod :: a }
  deriving (Eq, Ord)

modulo :: forall n a. (Integral a, KnownNat n) => a -> Mod a n
modulo a = Mod . mod a $ modulus @n

modulus :: forall n a. (Integral a, KnownNat n) => a
modulus = fromInteger $ natVal (Proxy @n)

instance Show a => Show (Mod a n) where
  show (Mod a) = show a

instance (Integral a, KnownNat n) => Num (Mod a n) where
  fromInteger = modulo . fromInteger

  Mod a + Mod b = modulo $ a + b
  Mod a * Mod b = modulo $ a * b

  abs    (Mod a) = modulo $ abs a
  signum (Mod a) = modulo $ signum a
  negate (Mod a) = modulo $ negate a

instance (Integral a, KnownNat n) => Enum (Mod a n) where
  succ (Mod x)
    | x == unMod (maxBound :: Mod a n) =
        errorWithoutStackTrace "Prelude.Enum.succ{Mod}: tried to take `succ' of maxBound"
    | otherwise           = Mod $ x + 1

  pred (Mod x)
    | x == unMod (minBound :: Mod a n) =
        errorWithoutStackTrace "Prelude.Enum.pred{Mod}: tried to take `pred' of minBound"
    | otherwise            = Mod $ x - 1

  toEnum = fromIntegral
  fromEnum = fromIntegral . unMod

  enumFrom     x   = enumFromTo     x maxBound
  enumFromThen x y = enumFromThenTo x y bound
    where
      bound | fromEnum y >= fromEnum x = maxBound
            | otherwise                = minBound

instance (Integral a, KnownNat n) => Bounded (Mod a n) where
  maxBound = Mod $ pred (modulus @n)
  minBound = 0

instance (Integral a, KnownNat n) => Real (Mod a n) where
  toRational (Mod a) = toInteger a % 1

instance (Integral a, KnownNat n) => Integral (Mod a n) where
  toInteger (Mod a) = toInteger a
  quotRem a b = (a * inv b, 0)

inv :: forall n a. (Integral a, KnownNat n) => Mod a n -> Mod a n
inv x = go (unMod x) (modulus @n) 1 0
  where go _ 0 u _ = modulo u
        go a b u v = go b (a - t * b) v (u - t * v)
          where t = a `div` b

type MInt = Mod Int 1000000007