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;

fn fail() {
    println!("{}", 0);
    std::process::exit(0);
}

fn fact(n: u32) -> ModInt {
    let mut x = ModInt::new(1);
    for i in 1..(n + 1) {
        x *= ModInt::new(i);
    }
    x
}

fn combi(n: u32, m: u32) -> ModInt {
    if n < m {
        ModInt::new(0)
    } else {
        fact(n) / fact(m) / fact(n - m)
    }
}

fn main() {
    let cin = stdin();
    let cin = cin.lock();
    let mut sc = Scanner::new(cin);

    let mut x: u32 = sc.next();
    let mut y: u32 = sc.next();

    if (x + y) % 3 != 0 {
        fail();
    }
    let n = (x + y) / 3;
    if x < n || y < n {
        fail();
    }
    x -= n;
    y -= n;

    println!("{}", combi(x + y, x));
}

/* ========== MotInt ========== */

const MOD: u32 = 1_000_000_007;

#[derive(Clone, Copy)]
struct ModInt(u32);

// Fundamental
#[allow(dead_code)]
impl ModInt {
    pub fn new(x: u32) -> ModInt {
        ModInt(x % MOD)
    }

    pub fn pow(mut self, mut n: u32) -> ModInt {
        let mut ret = ModInt::new(1);
        while n > 0 {
            if n & 1 == 1 {
                ret *= self;
            }
            self *= self;
            n >>= 1;
        }
        ret
    }
}

// Assign
impl std::ops::AddAssign for ModInt {
    fn add_assign(&mut self, rhs: ModInt) {
        self.0 += rhs.0;
        if self.0 >= MOD {
            self.0 -= MOD;
        };
    }
}

impl std::ops::SubAssign for ModInt {
    fn sub_assign(&mut self, rhs: ModInt) {
        self.0 += MOD - rhs.0;
        if self.0 >= MOD {
            self.0 -= MOD;
        };
    }
}

impl std::ops::MulAssign for ModInt {
    fn mul_assign(&mut self, rhs: ModInt) {
        self.0 = ((self.0 as u64) * (rhs.0 as u64) % (MOD as u64)) as u32;
    }
}

impl std::ops::DivAssign for ModInt {
    fn div_assign(&mut self, rhs: ModInt) {
        *self *= rhs.pow(MOD - 2);
    }
}

// Binary operator
impl std::ops::Add for ModInt {
    type Output = ModInt;
    fn add(mut self, rhs: ModInt) -> Self::Output {
        self += rhs;
        self
    }
}

impl std::ops::Sub for ModInt {
    type Output = ModInt;
    fn sub(mut self, rhs: ModInt) -> Self::Output {
        self -= rhs;
        self
    }
}

impl std::ops::Mul for ModInt {
    type Output = ModInt;
    fn mul(mut self, rhs: ModInt) -> Self::Output {
        self *= rhs;
        self
    }
}

impl std::ops::Div for ModInt {
    type Output = ModInt;
    fn div(mut self, rhs: ModInt) -> Self::Output {
        self /= rhs;
        self
    }
}

// Output
impl std::fmt::Display for ModInt {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

/* ========== Scanner ========== */

struct Scanner<R: Read> {
    reader: R,
}

#[allow(dead_code)]
impl<R: Read> Scanner<R> {
    fn new(reader: R) -> Scanner<R> {
        Scanner { reader: reader }
    }

    fn read<T: FromStr>(&mut self) -> Option<T> {
        let token = self
            .reader
            .by_ref()
            .bytes()
            .map(|c| c.unwrap() as char)
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>();
        if token.is_empty() {
            None
        } else {
            token.parse::<T>().ok()
        }
    }

    fn next<T: FromStr>(&mut self) -> T {
        if let Some(s) = self.read() {
            s
        } else {
            writeln!(stderr(), "Terminated with EOF").unwrap();
            std::process::exit(0);
        }
    }

    fn vec<T: FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.next()).collect()
    }

    fn char(&mut self) -> char {
        self.next::<String>().chars().next().unwrap()
    }
}