# Number of pairs of arrays (A, B) such that A is ascending, B is descending and A[i] ≤ B[i]

Given two integers **N** and **M**, the task is to find the number of pairs of arrays **(A, B)** such that array **A** and **B** both are of size **M** each where each entry of **A** and **B** is an integer between **1** and **N** such that for each **i** between **1** and **M**, **A[i] ≤ B[i]**. It is also given that the array **A** is sorted in **non-descending** order and **B** is sorted in **non-ascending **order. Since the answer can be very large, return answer modulo **10 ^{9} + 7**.

**Examples:**

Input:N = 2, M = 2

Output:5

1: A= [1, 1] B=[1, 1]

2: A= [1, 1] B=[1, 2]

3: A= [1, 1] B=[2, 2]

4: A= [1, 2] B=[2, 2]

5: A= [2, 2] B=[2, 2]

Input:N = 5, M = 3

Output:210

**Approach:** Notice that if there is a valid pair of arrays A and B and if B is concatenated after A the resultant array will always be either an ascending or a non-descending array of size of 2 * M. Each element of (A + B) will be between 1 and N (It is not necessary that all elements between 1 and N have to be used). This now simply converts the given problem to finding all the possible combinations of size **2 * M** where each element is between 1 to N (with repetitions allowed) whose formula is ** ^{2 * M + N – 1}C_{N – 1}** or (2 * M + N – 1)! / ((2 * M)! * (N – 1)!).

Below is the implementation of the above approach:

## CPP

`// C++ code of above approach ` `#include <bits/stdc++.h> ` `#define mod 1000000007 ` `using` `namespace` `std; ` ` ` `long` `long` `fact(` `long` `long` `n) ` `{ ` ` ` `if` `(n == 1) ` ` ` `return` `1; ` ` ` `else` ` ` `return` `(fact(n - 1) * n) % mod; ` `} ` ` ` `// Function to return the count of pairs ` `long` `long` `countPairs(` `int` `m, ` `int` `n) ` `{ ` ` ` `long` `long` `ans = fact(2 * m + n - 1) / ` ` ` `(fact(n - 1) * fact(2 * m)); ` ` ` `return` `(ans % mod); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 5, m = 3; ` ` ` `cout << (countPairs(m, n)); ` ` ` `return` `0; ` `} ` ` ` `// This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## Java

`// Java code of above approach ` `class` `GFG ` `{ ` ` ` `final` `static` `long` `mod = ` `1000000007` `; ` ` ` ` ` `static` `long` `fact(` `long` `n) ` ` ` `{ ` ` ` `if` `(n == ` `1` `) ` ` ` `return` `1` `; ` ` ` `else` ` ` `return` `(fact(n - ` `1` `) * n) % mod; ` ` ` `} ` ` ` ` ` `// Function to return the count of pairs ` ` ` `static` `long` `countPairs(` `int` `m, ` `int` `n) ` ` ` `{ ` ` ` `long` `ans = fact(` `2` `* m + n - ` `1` `) / ` ` ` `(fact(n - ` `1` `) * fact(` `2` `* m)); ` ` ` ` ` `return` `(ans % mod); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `5` `, m = ` `3` `; ` ` ` ` ` `System.out.println(countPairs(m, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `from` `math ` `import` `factorial as fact ` ` ` `# Function to return the count of pairs ` `def` `countPairs(m, n): ` ` ` `ans ` `=` `fact(` `2` `*` `m ` `+` `n` `-` `1` `)` `/` `/` `(fact(n` `-` `1` `)` `*` `fact(` `2` `*` `m)) ` ` ` `return` `(ans ` `%` `(` `10` `*` `*` `9` `+` `7` `)) ` ` ` `# Driver code ` `n, m ` `=` `5` `, ` `3` `print` `(countPairs(m, n)) ` |

*chevron_right*

*filter_none*

## C#

`// C# code of above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `static` `long` `mod = 1000000007 ; ` ` ` ` ` `static` `long` `fact(` `long` `n) ` ` ` `{ ` ` ` `if` `(n == 1) ` ` ` `return` `1; ` ` ` `else` ` ` `return` `(fact(n - 1) * n) % mod; ` ` ` `} ` ` ` ` ` `// Function to return the count of pairs ` ` ` `static` `long` `countPairs(` `int` `m, ` `int` `n) ` ` ` `{ ` ` ` `long` `ans = fact(2 * m + n - 1) / ` ` ` `(fact(n - 1) * fact(2 * m)); ` ` ` ` ` `return` `(ans % mod); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 5, m = 3; ` ` ` ` ` `Console.WriteLine(countPairs(m, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

210

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Sort Matrix in alternating ascending and descending order rowwise
- Sort first half in ascending and second half in descending order | Set 2
- Sort first half in ascending and second half in descending order | 1
- Given two arrays count all pairs whose sum is an odd number
- Sort first k values in ascending order and remaining n-k values in descending order
- Distinct pairs from given arrays (a[i], b[j]) such that (a[i] + b[j]) is a Fibonacci number
- Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first
- Sort all even numbers in ascending order and then sort all odd numbers in descending order
- Number of ways to select equal sized subarrays from two arrays having atleast K equal pairs of elements
- Count Pairs from two arrays with even sum
- Maximizing Unique Pairs from two arrays
- Given two unsorted arrays, find all pairs whose sum is x
- Number of pairs such that path between pairs has the two vertices A and B
- Count equal pairs from given string arrays
- Minimum sum of absolute difference of pairs of two arrays
- Count of pairs between two arrays such that the sums are distinct
- Find k pairs with smallest sums in two arrays
- Count distinct pairs from two arrays having same sum of digits
- Count pairs from two sorted arrays whose sum is equal to a given value x
- Find k pairs with smallest sums in two arrays | Set 2

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.