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)

const MOD_N: u64 = 1000000007;

fn inv(mut a: u64) -> u64 {
    // a^(MOD_N-2)

    let mut n = MOD_N - 2;
    let mut x = 1;

    while n > 0 {
        if n % 2 == 1 {
            x = (x * a) % MOD_N;
        }

        a = (a * a) % MOD_N;

        n /= 2;
    }

    x
}

fn comb(n: usize, k: usize) -> usize {
    let n = n as u64;
    let k = k as u64;
    let mut v = 1u64;

    for i in 0..k {
        v = (v * (n-i)) % MOD_N;
    }

    for i in 1..k {
        v = (v *  inv(i+1)) % MOD_N;
    }

    v as usize
}

fn solve(x: usize, y: usize) -> usize {
    let xy_max = std::cmp::max(x, y);
    let xy_min = std::cmp::min(x, y);

    // 3n + 2m = xy_max
    // 3n +  m = xy_min

    let m = xy_max - xy_min;

    if xy_min * 2 < xy_max {
        return 0;
    }

    let t = 2 * xy_min - xy_max;
    if t % 3 != 0 {
        return 0;
    }

    let n = t / 3;

    let nr_moves_all = n * 2 + m;
    let nr_moves_min = n;

    comb(nr_moves_all, nr_moves_min)
}

fn main() {
    let mut buf = String::new();
    std::io::stdin().read_line(&mut buf).unwrap();
    let mut iter = buf.split_whitespace();
    let x: usize = iter.next().unwrap().parse().unwrap();
    let y: usize = iter.next().unwrap().parse().unwrap();


    println!("{}", solve(x, y));
}