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-mcs 6.8.0.105) (judge verdict: Accepted)

using static System.Math;
using static System.Console;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;

namespace AtCoder
{
    class Program
    {
        private static long mod = 1000000007;
        static void Main(string[] args)
        {
            var line = ReadLine().Split(' ').Select(value => long.Parse(value)).ToArray();
            var x = line[0];
            var y = line[1];
            //var a = long.Parse(ReadLine());

            if(2 * x < y || 2 * y < x)
            {
                WriteLine(0);
                return;
            }

            var total = x + y;
            if(total % 3 != 0)
            {
                WriteLine(0);
                return;
            }

            total /= 3;

            var fac = new long[total + 1];
            var inv = new long[total + 1];
            var facInv = new long[total + 1];

            Init(fac, inv, facInv, total);

            long count = 0;
            for(var i = 0; i <= total; i++)
            {
                if(i - (total - i) != -x + y)
                {
                    continue;
                }
                count += Combination(fac, inv, facInv, total, i);
                count %= mod;
            }

            WriteLine(count);
        }

        private static void Init(long[] fac, long[] inv, long[] facInv, long n)
        {
            fac[0] = fac[1] = 1;
            facInv[0] = facInv[1] = 1;
            inv[1] = 1;

            for(var i = 2; i <= n; i++)
            {
                fac[i] = fac[i - 1] * i % mod;
                inv[i] = mod - inv[mod % i] * (mod / i) % mod;
                facInv[i] = facInv[i - 1] * inv[i] % mod;
            }
        }

        private static long Combination(long[] fac, long[] inv, long[] facInv, long n, long r)
        {
            return fac[n] * (facInv[r] * facInv[n - r] % mod) % mod;
        }
    }
}