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 C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using static Constans;

public static class Constans
{
    public const long Mod = (long)1e9 + 7;
}

public class Combination
{
    private readonly long[] Fact;
    private readonly long[] InvFact;  // モジュラ逆数

    public Combination(long n)
    {
        Fact = new long[n + 1];
        InvFact = new long[n + 1];

        long f = 1;
        Fact[0] = f;
        for (long i = 1; i <= n; i++)
        {
            f *= i;
            f %= Mod;
            Fact[i] = f;
        }

        long inv = ModInt.GetInv(f).Num;

        InvFact[n] = inv;
        for (long i = n - 1; i >= 0; i--)
        {
            InvFact[i] = (InvFact[i + 1] * (i + 1)) % Mod;
        }
    }

    // nCr
    public ModInt GetCombination(long n, long r)
    {
        var comb = Fact[n] * InvFact[r] % Mod;
        comb = comb * InvFact[n - r] % Mod;
        return new ModInt(comb);
    }
}

public struct ModInt
{
    public long Num;

    public ModInt(long num)
    {
        Num = num;
    }

    // x^n
    public static long Power(long x, long n)
    {
        long pow = 1;
        while (n > 0)
        {
            if (n % 2 == 0)
            {
                x *= x;
                x %= Mod;
                n /= 2;
            }
            else
            {
                pow *= x;
                pow %= Mod;
                n--;
            }
        }

        return pow;
    }

    public static ModInt GetInv(long f)
    {
        // フェルマーの小定理 a^(-1) = a^(mod-2)
        return new ModInt(Power(f, Mod - 2));
    }

    public ModInt GetInv()
    {
        return GetInv(Num);
    }

    public override string ToString()
    {
        return Num.ToString();
    }

    public static ModInt operator +(ModInt l, ModInt r)
    {
        l.Num += r.Num;
        if (l.Num >= Mod)
        {
            l.Num -= Mod;
        }
        return l;
    }

    public static ModInt operator -(ModInt l, ModInt r)
    {
        l.Num -= r.Num;
        if (l.Num < 0)
        {
            l.Num += Mod;
        }
        return l;
    }

    public static ModInt operator *(ModInt l, ModInt r)
    {
        return new ModInt(l.Num * r.Num % Mod);
    }

    public static implicit operator ModInt(long n)
    {
        n %= Mod;
        if (n < 0)
        {
            n += Mod;
        }
        return new ModInt(n);
    }
}

public class Solution
{
    public static void Main()
    {
        var vals = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        var X = vals[0];
        var Y = vals[1];
        // 左下へ
        var ly = X + Y;
        // y = 2x と y = -x + ly の交点を求める
        // 3x = ly
        if (ly % 3 != 0)
        {
            Console.WriteLine(0);
            return;
        }
        var cx = ly / 3;
        var cy = 2 * cx;
        if (!(cx <= X && X <= cy && cx <= Y && Y <= cy))
        {
            Console.WriteLine(0);
            return;
        }
        // パスカルの三角形より値を得る
        var n = cx + 1;
        var k = X - cx + 1;
        var pattern = new Combination(n).GetCombination(n - 1, k - 1);

        Console.WriteLine(pattern);
    }
}