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 Rust (1.15.1) (judge verdict: Accepted)

fn read_stdin() -> String {
    use std::io::Read;
    let mut input = String::new();
    std::io::stdin().read_to_string(&mut input).unwrap();
    input
}

fn scan<'a, I: Iterator<Item = &'a str>, T: std::str::FromStr>(tokens: &mut I) -> T {
    tokens.next().unwrap().parse().ok().unwrap()
}

fn main() {
    let input = read_stdin();
    let tokens = &mut input.split_whitespace();
    let x: usize = scan(tokens);
    let y: usize = scan(tokens);
    if (x + y) % 3 != 0 {
        println!("0");
        return;
    }
    let n = (x + y) / 3;
    if x < n || y < n {
        println!("0");
        return;
    }
    println!("{}", binom(x + y - 2 * n, x - n).value());
}

const MODULO: usize = 1_000_000_007;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct F(usize);

impl F {
    fn value(self) -> usize {
        self.0
    }
    fn pow(self, e: usize) -> F {
        let mut a = F(1);
        let mut b = self;
        let mut e = e;
        while e != 0 {
            if e & 1 != 0 {
                a = a * b;
            }
            b = b * b;
            e >>= 1;
        }
        a
    }
    fn inverse(self) -> F {
        self.pow(MODULO - 2)
    }
}

impl std::ops::Add for F {
    type Output = F;
    fn add(self, other: F) -> F {
        F((self.value() + other.value()) % MODULO)
    }
}

impl std::ops::Sub for F {
    type Output = F;
    fn sub(self, other: F) -> F {
        F((self.value() + MODULO - other.value()) % MODULO)
    }
}

impl std::ops::Mul for F {
    type Output = F;
    fn mul(self, other: F) -> F {
        F((self.value() * other.value()) % MODULO)
    }
}

impl std::ops::Div for F {
    type Output = F;
    fn div(self, other: F) -> F {
        self * other.inverse()
    }
}

fn binom(n: usize, k: usize) -> F {
    let k = std::cmp::min(k, n - k);
    let mut numer = F(1);
    let mut denom = F(1);
    for i in 0..k {
        numer = numer * F(n - i);
        denom = denom * F(k - i);
    }
    numer / denom
}