# Number of ways to distribute N Paper Set among M students

Given **N** students and a total of **M** sets of question paper where **M ≤ N**. All the **M** sets are different and every sets is available in sufficient quantity. All the students are sitting in a single row. The task is to find the number of ways to distribute the question paper so that if any **M** consecutive students are selected then each student has a unique question paper set. The answer could be large, so print the answer modulo **10 ^{9} + 7**.

**Example:**

Input:N = 2, M = 2

Output:2

(A, B) and (B, A) are the only possible ways.

Input:N = 15, M = 4

Output:24

**Approach:** It can be observed that the number of ways are independent of **N** and only depend on **M**. First **M** students can be given **M** sets and then the same pattern can be repeated. The number of ways to distribute the question paper in this way is **M!**. For example,

N = 6, M = 3

A, B, C, A, B, C

A, C, B, A, C, B

B, C, A, B, C, A

B, A, C, B, A, C

C, A, B, C, A, B

C, B, A, C, B, A

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `const` `int` `MOD = 1000000007; ` ` ` `// Function to return n! % 1000000007 ` `int` `factMod(` `int` `n) ` `{ ` ` ` ` ` `// To store the factorial ` ` ` `long` `fact = 1; ` ` ` ` ` `// Find the factorial ` ` ` `for` `(` `int` `i = 2; i <= n; i++) { ` ` ` `fact *= (i % MOD); ` ` ` `fact %= MOD; ` ` ` `} ` ` ` ` ` `return` `fact; ` `} ` ` ` `// Function to return the ` `// count of possible ways ` `int` `countWays(` `int` `n, ` `int` `m) ` `{ ` ` ` `return` `factMod(m); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 2, m = 2; ` ` ` ` ` `cout << countWays(n, m); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` `static` `int` `MOD = ` `1000000007` `; ` ` ` `// Function to return n! % 1000000007 ` `static` `int` `factMod(` `int` `n) ` `{ ` ` ` ` ` `// To store the factorial ` ` ` `long` `fact = ` `1` `; ` ` ` ` ` `// Find the factorial ` ` ` `for` `(` `int` `i = ` `2` `; i <= n; i++) ` ` ` `{ ` ` ` `fact *= (i % MOD); ` ` ` `fact %= MOD; ` ` ` `} ` ` ` `return` `(` `int` `)fact; ` `} ` ` ` `// Function to return the ` `// count of possible ways ` `static` `int` `countWays(` `int` `n, ` `int` `m) ` `{ ` ` ` `return` `factMod(m); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `n = ` `2` `, m = ` `2` `; ` ` ` ` ` `System.out.print(countWays(n, m)); ` `} ` `} ` ` ` `// This code is contributed by Arnab Kundu ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `MOD ` `=` `1000000007` `; ` ` ` `# Function to return n! % 1000000007 ` `def` `factMod(n) : ` ` ` ` ` `# To store the factorial ` ` ` `fact ` `=` `1` `; ` ` ` ` ` `# Find the factorial ` ` ` `for` `i ` `in` `range` `(` `2` `, n ` `+` `1` `) : ` ` ` `fact ` `*` `=` `(i ` `%` `MOD); ` ` ` `fact ` `%` `=` `MOD; ` ` ` ` ` `return` `fact; ` ` ` `# Function to return the ` `# count of possible ways ` `def` `countWays(n, m) : ` ` ` ` ` `return` `factMod(m); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `2` `; m ` `=` `2` `; ` ` ` ` ` `print` `(countWays(n, m)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

2

## Recommended Posts:

- Count ways to distribute m items among n people
- Minimum number of mails required to distribute all the questions
- Minimum and maximum number of N chocolates after distribution among K students
- Distribute N candies among K people
- Distribute candies in a Binary Tree
- Paper Cut into Minimum Number of Squares
- Number of ways to split a binary number such that every part is divisible by 2
- Number of ways to calculate a target number using only array elements
- Find the number of ways to divide number into four parts such that a = c and b = d
- Count number of ways to divide a number in 4 parts
- Number of wins for each player in a series of Rock-Paper-Scissor game
- Number of ways to choose a pair containing an even and an odd number from 1 to N
- Number of ways to get a given sum with n number of m-faced dices
- Count number of ways to get Odd Sum
- Why is programming important for first year or school students?

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.