Given two integers **X** and **Y**. The task is to find the number of ways to reach **(X, Y)** in a matrix starting from the origin when the possible moves are from **(i, j)** to either **(i + 1, j + 2)** or **(i + 2, j + 1)**. Rows are numbered from top to bottom and columns are numbered from left to right. The answer could be large, so print the answer modulo **10 ^{9} + 7**

**Examples:**

Input:X = 3, Y = 3

Output:2

The only possible ways are (0, 0) -> (1, 2) -> (3, 3)

and (0, 0) -> (2, 1) -> (3, 3)

Input:X = 2, Y = 3

Output:0

**Approach:** The value of **x** coordinate + **y** coordinate increases by **3** with one movement. So when **X + Y** is not a multiple of **3**, the answer is **0**. When the number of movements of **(+1, +2)** is **n** and the number of movements of **(+2, +1)** is **m** then **n + 2m = X**, **2n + m = Y**. The answer is **0** when **n < 0** or **m < 0**. If not, the answer is ^{n + m} C _{n} because it is only necessary to decide which **n + 1** of the total **n + m** moves **(+ 1, + 2)**. This value can be calculated by **O(n + m + log mod)** by calculating the factorial and its inverse. It can also be calculated with **O(min {n, m})**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `#define N 1000005 ` `#define mod (int)(1e9 + 7) ` ` ` `// To store the factorial and factorial ` `// mod inverse of the numbers ` `int` `factorial[N], modinverse[N]; ` ` ` `// Function to find (a ^ m1) % mod ` `int` `power(` `int` `a, ` `int` `m1) ` `{ ` ` ` `if` `(m1 == 0) ` ` ` `return` `1; ` ` ` `else` `if` `(m1 == 1) ` ` ` `return` `a; ` ` ` `else` `if` `(m1 == 2) ` ` ` `return` `(1LL * a * a) % mod; ` ` ` `else` `if` `(m1 & 1) ` ` ` `return` `(1LL * a * power(power(a, m1 / 2), 2)) % mod; ` ` ` `else` ` ` `return` `power(power(a, m1 / 2), 2) % mod; ` `} ` ` ` `// Function to find the factorial ` `// of all the numbers ` `void` `factorialfun() ` `{ ` ` ` `factorial[0] = 1; ` ` ` `for` `(` `int` `i = 1; i < N; i++) ` ` ` `factorial[i] = (1LL * factorial[i - 1] * i) % mod; ` `} ` ` ` `// Function to find the factorial ` `// modinverse of all the numbers ` `void` `modinversefun() ` `{ ` ` ` `modinverse[N - 1] = power(factorial[N - 1], mod - 2) % mod; ` ` ` ` ` `for` `(` `int` `i = N - 2; i >= 0; i--) ` ` ` `modinverse[i] = (1LL * modinverse[i + 1] * (i + 1)) % mod; ` `} ` ` ` `// Function to return nCr ` `int` `binomial(` `int` `n, ` `int` `r) ` `{ ` ` ` `if` `(r > n) ` ` ` `return` `0; ` ` ` ` ` `int` `a = (1LL * factorial[n] ` ` ` `* modinverse[n - r]) ` ` ` `% mod; ` ` ` ` ` `a = (1LL * a * modinverse[r]) % mod; ` ` ` `return` `a; ` `} ` ` ` `// Function to return the number of ways ` `// to reach (X, Y) in a matrix with the ` `// given moves starting from the origin ` `int` `ways(` `int` `x, ` `int` `y) ` `{ ` ` ` `factorialfun(); ` ` ` `modinversefun(); ` ` ` ` ` `if` `((2 * x - y) % 3 == 0 ` ` ` `&& (2 * y - x) % 3 == 0) { ` ` ` `int` `m = (2 * x - y) / 3; ` ` ` `int` `n = (2 * y - x) / 3; ` ` ` `return` `binomial(n + m, n); ` ` ` `} ` ` ` ` ` `return` `0; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `x = 3, y = 3; ` ` ` ` ` `cout << ways(x, y); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `N ` `=` `1000005` `mod ` `=` `(` `int` `)(` `1e9` `+` `7` `) ` ` ` `# To store the factorial and factorial ` `# mod inverse of the numbers ` `factorial ` `=` `[` `0` `] ` `*` `N; ` `modinverse ` `=` `[` `0` `] ` `*` `N; ` ` ` `# Function to find (a ^ m1) % mod ` `def` `power(a, m1) : ` ` ` ` ` `if` `(m1 ` `=` `=` `0` `) : ` ` ` `return` `1` `; ` ` ` `elif` `(m1 ` `=` `=` `1` `) : ` ` ` `return` `a; ` ` ` `elif` `(m1 ` `=` `=` `2` `) : ` ` ` `return` `(a ` `*` `a) ` `%` `mod; ` ` ` `elif` `(m1 & ` `1` `) : ` ` ` `return` `(a ` `*` `power(power(a, m1 ` `/` `/` `2` `), ` `2` `)) ` `%` `mod; ` ` ` `else` `: ` ` ` `return` `power(power(a, m1 ` `/` `/` `2` `), ` `2` `) ` `%` `mod; ` ` ` `# Function to find the factorial ` `# of all the numbers ` `def` `factorialfun() : ` ` ` ` ` `factorial[` `0` `] ` `=` `1` `; ` ` ` `for` `i ` `in` `range` `(` `1` `, N) : ` ` ` `factorial[i] ` `=` `(factorial[i ` `-` `1` `] ` `*` `i) ` `%` `mod; ` ` ` `# Function to find the factorial ` `# modinverse of all the numbers ` `def` `modinversefun() : ` ` ` ` ` `modinverse[N ` `-` `1` `] ` `=` `power(factorial[N ` `-` `1` `], ` ` ` `mod ` `-` `2` `) ` `%` `mod; ` ` ` ` ` `for` `i ` `in` `range` `(N ` `-` `2` `, ` `-` `1` `, ` `-` `1` `) : ` ` ` `modinverse[i] ` `=` `(modinverse[i ` `+` `1` `] ` `*` ` ` `(i ` `+` `1` `)) ` `%` `mod; ` ` ` `# Function to return nCr ` `def` `binomial(n, r) : ` ` ` ` ` `if` `(r > n) : ` ` ` `return` `0` `; ` ` ` ` ` `a ` `=` `(factorial[n] ` `*` `modinverse[n ` `-` `r]) ` `%` `mod; ` ` ` ` ` `a ` `=` `(a ` `*` `modinverse[r]) ` `%` `mod; ` ` ` `return` `a; ` ` ` `# Function to return the number of ways ` `# to reach (X, Y) in a matrix with the ` `# given moves starting from the origin ` `def` `ways(x, y) : ` ` ` ` ` `factorialfun(); ` ` ` `modinversefun(); ` ` ` ` ` `if` `((` `2` `*` `x ` `-` `y) ` `%` `3` `=` `=` `0` `and` ` ` `(` `2` `*` `y ` `-` `x) ` `%` `3` `=` `=` `0` `) : ` ` ` `m ` `=` `(` `2` `*` `x ` `-` `y) ` `/` `/` `3` `; ` ` ` `n ` `=` `(` `2` `*` `y ` `-` `x) ` `/` `/` `3` `; ` ` ` ` ` `return` `binomial(n ` `+` `m, n); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `x ` `=` `3` `; y ` `=` `3` `; ` ` ` ` ` `print` `(ways(x, y)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Number of ways to reach (M, N) in a matrix starting from the origin without visiting (X, Y)
- Count of ways to traverse a Matrix and return to origin in K steps
- Counts paths from a point to reach Origin
- Check if it is possible to reach (x, y) from origin in exactly Z steps using only plus movements
- Minimum number of moves to reach N starting from (1, 1)
- Number of ways to reach the end of matrix with non-zero AND value
- Count number of ways to reach a given score in a Matrix
- Count number of ways to reach destination in a Maze
- Number of ways to reach Nth floor by taking at-most K leaps
- Count number of ways to reach destination in a maze
- Count number of ways to reach destination in a Maze using BFS
- Find the number of ways to reach Kth step in stair case
- Print a matrix in a spiral form starting from a point
- Element in a matrix starting from which anti-clockwise traversal ends at the last element
- Final direction after visiting every cell of Matrix starting from (0, 0)
- Count ways to reach the n'th stair
- Count ways to reach the nth stair using step 1, 2 or 3
- Count ways to reach the Nth stair using multiple 1 or 2 steps and a single step 3
- Count of all possible ways to reach a target by a Knight
- Expected number of moves to reach the end of a board | Matrix Exponentiation

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.