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*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `static` `int` `MOD = 1000000007; ` ` ` ` ` `// Function to return n! % 1000000007 ` ` ` `static` `int` `factMod(` `int` `n) ` ` ` `{ ` ` ` `// To store the factorial ` ` ` `int` `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 ` ` ` `static` `int` `countWays(` `int` `n, ` `int` `m) ` ` ` `{ ` ` ` `return` `factMod(m); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 2, m = 2; ` ` ` `Console.Write(countWays(n, m)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Sanjit Prasad ` |

*chevron_right*

*filter_none*

**Output:**

2

## Recommended Posts:

- Count ways to distribute m items among n people
- Count of ways to distribute N items among 3 people with one person receiving maximum
- Distribute N candies among K people
- Minimum and maximum number of N chocolates after distribution among K students
- Count ways to distribute exactly one coin to each worker
- Count the number of ways to give ranks for N students such that same ranks are possible
- Minimum number of mails required to distribute all the questions
- Distribute candies in a Binary Tree
- Find ways to arrange K green balls among N balls such that exactly i moves is needed to collect all K green balls
- Why is programming important for first year or school students?
- Maximum students to pass after giving bonus to everybody and not exceeding 100 marks
- Find Non-overlapping intervals among a given set of intervals
- Find position of the given number among the numbers made of 4 and 7
- Paper Cut into Minimum Number of Squares
- Number of wins for each player in a series of Rock-Paper-Scissor game
- Sum of bit differences among all pairs
- Repeated subtraction among two numbers
- Find maximum among x^(y^2) or y^(x^2) where x and y are given
- Position of n among the numbers made of 2, 3, 5 & 7
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K

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.