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: Wrong Answer)

use std::vec::Vec;
use std::collections::HashMap;
use std::cmp::*;
use std::ops::Add;
use std::ops::Sub;
use std::ops::Mul;
use std::ops::Neg;
use std::ops::Div;


fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>().split_whitespace().map(|e| e.parse().ok().unwrap()).collect()
}

fn read_touple<T: std::str::FromStr + Copy>() -> (T, T) {
  let v: Vec<T> = read_vec();
  assert_eq!(v.len(), 2);
  (v[0], v[1])
}


fn main() {
    let (x, y) = read_touple::<i64>();
    if (x + y) % 3 != 0  {
        println!("{}", 0);
        return
    } 

    let (m, n) = ((2*x - y)/3, (-x + 2*y)/3);
    if m < 0 && n < 0 {
        println!("{}", 0);
        return
    } 

    let m = Mod::new((m + n) as usize);
    let n = Mod::new(n as usize);

    let ans = m.conb(n).get_val();
    println!("{}", ans);
}

const MOD: usize = 1_000_000_000 + 7;

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
struct Mod {
  val: usize,
}


// 和を定義
impl Add for Mod {
  type Output = Mod;
  // O(1)
  fn add(self, other: Mod) -> Mod {
    Mod { val: (self.val + other.val) % MOD }
  }
}

// 差を定義
impl Sub for Mod {
  type Output = Mod;

  // O(1)
  fn sub(self, other: Mod) -> Mod {
    let mx = max(self.val, other.val);
    let mn = min(self.val, other.val);

    Mod { val: (mx - mn) % MOD }
  }
}

// 積を定義
impl Mul for Mod {
  type Output = Mod;

  // O(1)
  fn mul(self, other: Mod) -> Mod {
    Mod { val: (self.val * other.val) % MOD }
  }
}

// 加法単位元
impl Neg for Mod {
  type Output = Mod;

  // O(1)
  fn neg(self) -> Mod {
    Mod { val: MOD - self.val }
  }
}

// 逆元を定義
// フェルマーの小定理により実装
impl Div for Mod {
  type Output = Mod;

  //O(logN)
  fn div(self, other: Mod) -> Mod {
    self * other.inv()
  }
}

impl Mod {


  // コンストラクター
  fn new(n: usize) -> Mod {
    Mod { val: n %  MOD }
  }

  // 加法単位元
  fn new_zero() -> Mod {
    Mod { val: 0 }
  }

  // 乗法単位元
  fn new_one() -> Mod {
    Mod { val: 1 }
  }

  // 加法逆元
  // 上記にて定義



  // 乗法逆元
  fn inv(self) -> Mod {
    if self.val == 0 { panic!("zero divide"); }
    self.pow(MOD - 2)
  }

  // 累乗を定義
  // O(logN)
  fn pow(self, n: usize) -> Mod {
    let mut res = Mod::new(1);
    let mut a = self;
    let mut n = n;

    while n > 0 {
      if n % 2 == 0 {
        a = a * a;
        n /= 2;
      } else {
        res = res * a;
        a = a * a;
        n /= 2;
      }
    }
    res
  }

  // 最大公約数
  // O(logN)
  fn gcd(self, other: Mod) -> usize {
      let mut x = self.val;
      let mut y = other.val;
      while y != 0 {
          let t = y;
          y = x % y;
          x = t;
      }
      x
  }

  // 階乗
  // O(N)
  fn fact(self) -> Mod {
    let mut res = Mod::new_one();
    for i in 1..self.val+1 {
      res = res * Mod::new(i)
    }

    res
  }

  // 順列
  fn perm(self, other: Mod) -> Mod {
    let mut res = Mod::new_one();
    for x in (self.val - other.val + 1)..self.val+1 {
      res = res * Mod::new(x);
    }

    res
  }

  // 組み合わせ O(N)
  fn conb(self, other: Mod) -> Mod {
    self.fact() / (other.fact() * (self - other).fact())
  }

  fn get_val(self) -> usize { self.val }
}