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)

use std::io::*;
use std::str::FromStr;

const MOD: i64 = 1_000_000_007;

fn mod_pow(x: i64, n: i64, mod_: i64) -> i64 {
    if n > 0 {
        mod_pow(x * x % mod_, n >> 1, mod_) * if n & 1 == 1 { x } else { 1_i64 } % mod_
    } else {
        1_i64
    }
}

struct ModCulc {
    fact: Vec<i64>,
    fact_inv: Vec<i64>,
    mod_: i64,
}

impl ModCulc {
    fn new(n: usize, mod_: i64) -> Self {
        let mut f: Vec<i64> = vec![1; n];
        let mut f_i: Vec<i64> = vec![1; n];
        for i in 1..n {
            f[i] = (f[i - 1] * i as i64) % mod_;
            f_i[i] = mod_pow(f[i], mod_ - 2, mod_);
        }
        ModCulc {
            fact: f,
            fact_inv: f_i,
            mod_: mod_,
        }
    }

    fn nCr(&self, n: usize, r: usize) -> i64 {
        ((self.fact[n] * self.fact_inv[r]) % self.mod_ * self.fact_inv[n - r]) % self.mod_
    }
}

fn main() {
    let x: usize = read();
    let y: usize = read();

    if (x + y) % 3 != 0 {
        println!("0");
        return;
    } else {
        let turn = (x + y) / 3;
        if turn > x || turn > y {
            println!("0");
            return;
        }
        let dx = x - turn;
        let dy = y - turn;
        let mc = ModCulc::new(dx+dy+1, MOD);
        println!("{}", mc.nCr(dx+dy, dx));
    }
}

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().unwrap()
}