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: Time Limit Exceeded)

use std::cmp;

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
  }

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
  }

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };
    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };
    // 3
    // 1 2
    // 1 3
    // 1 4
    // のようなタイプ
    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    }};
    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
  }

fn main() {
    input! {
        x: u64,
        y: u64
    }

    let total = x + y;

    if total % 3 != 0 {
        println!("0");
    } else {
        // +1,+2の移動をa +2,+1の移動をbとする
        // a + 2b = X
        // 2a + b = Y

        // 2Y - X = 3a
        // a = ( 2Y -X ) / 3
        // b = ( 2X -Y ) / 3

        let a = (2 * y - x) / 3;
        let b = (2 * x - y) / 3;

        //println!("{} {}", a, b);

        if a < 0 || b < 0 {
            println!("0");
        } else {
            let total_move = a + b;

            // 前処理
            let max: usize = a as usize + b as usize + 1;
            let modulo = 1000000007;
            let mut fac = Vec::new();
            let mut finv = Vec::new();
            let mut inv = Vec::new();

            for i in 0..2 {
                fac.push(1);
                finv.push(1);
                inv.push(1);
            }

            for i in 2..max {
                let faci = fac[i - 1];
                let invmi = inv[modulo % i];
                let finvim1 = finv[i - 1];
                fac.push(faci * i % modulo);
                inv.push(modulo - invmi * (modulo / i) % modulo);
                finv.push(finvim1 * inv[i] % modulo);
            }

            fn com(
                n: usize,
                k: usize,
                modulo: usize,
                fac: &Vec<usize>,
                finv: &Vec<usize>,
            ) -> usize {
                if n < k {
                    return 0;
                };

                if n < 0 || k < 0 {
                    return 0;
                }
                return fac[n] * (finv[k] * finv[n - k] % modulo) % modulo;
            }

            println!("{}", com((a + b) as usize, b as usize, modulo, &fac, &finv));
        }
    }
}