# Color all boxes in line such that every M consecutive boxes are unique

Given **N** boxes that are kept in a straight line and **M** colors such that **M ≤ N**. The position of the boxes cannot be changed. The task is to find the number of ways to color the boxes such that if any **M** consecutive set of boxes is considered then the color of each box is unique. Since the answer could be large, print the answer modulo 10^{9} + 7.

**Example:**

Input:N = 3, M = 2

Output:2

If colours are c1 and c2 the only possible

ways are {c1, c2, c1} and {c2, c1, c2}.

Input:N = 13, M = 10

Output:3628800

**Approach:** The number of ways are independent of **N** and only depends on **M**. First **M** boxes can be coloured with the given **M** colours without repetition then the same pattern can be repeated for the next set of **M** boxes. This can be done for every permutation of the colours. So, the number of ways to color the boxes will be **M!**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `#define MOD 1000000007 ` ` ` `// Function to return (m! % MOD) ` `int` `modFact(` `int` `n, ` `int` `m) ` `{ ` ` ` `int` `result = 1; ` ` ` `for` `(` `int` `i = 1; i <= m; i++) ` ` ` `result = (result * i) % MOD; ` ` ` ` ` `return` `result; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 3, m = 2; ` ` ` ` ` `cout << modFact(n, m); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` `class` `GFG ` `{ ` ` ` `static` `final` `int` `MOD = ` `1000000007` `; ` ` ` ` ` `// Function to return (m! % MOD) ` ` ` `static` `int` `modFact(` `int` `n, ` `int` `m) ` ` ` `{ ` ` ` `int` `result = ` `1` `; ` ` ` `for` `(` `int` `i = ` `1` `; i <= m; i++) ` ` ` `result = (result * i) % MOD; ` ` ` ` ` `return` `result; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `3` `, m = ` `2` `; ` ` ` ` ` `System.out.println(modFact(n, m)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `MOD ` `=` `1000000007` ` ` `# Function to return (m! % MOD) ` `def` `modFact(n, m) : ` ` ` ` ` `result ` `=` `1` ` ` `for` `i ` `in` `range` `(` `1` `, m ` `+` `1` `) : ` ` ` `result ` `=` `(result ` `*` `i) ` `%` `MOD ` ` ` ` ` `return` `result ` ` ` `# Driver code ` `n ` `=` `3` `m ` `=` `2` ` ` `print` `(modFact(n, m)) ` ` ` `# This code is contributed by ` `# divyamohan123 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` `class` `GFG ` `{ ` ` ` `const` `int` `MOD = 1000000007; ` ` ` ` ` `// Function to return (m! % MOD) ` ` ` `static` `int` `modFact(` `int` `n, ` `int` `m) ` ` ` `{ ` ` ` `int` `result = 1; ` ` ` `for` `(` `int` `i = 1; i <= m; i++) ` ` ` `result = (result * i) % MOD; ` ` ` ` ` `return` `result; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 3, m = 2; ` ` ` ` ` `Console.WriteLine(modFact(n, m)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Nidhi_biet ` |

*chevron_right*

*filter_none*

**Output:**

2

## Recommended Posts:

- Color N boxes using M colors such that K boxes have different color from the box on its left
- Find the number of boxes to be removed
- Number of visible boxes after putting one inside another
- Total number of different staircase that can made from N boxes
- Minimum number of stacks possible using boxes of given capacities
- Queries to check if it is possible to join boxes in a circle
- Minimum boxes required to carry all gifts
- Minimum time required to transport all the boxes from source to the destination under the given constraints
- Equation of straight line passing through a given point which bisects it into two equal line segments
- RGYB(color) Slots Game to guess the correct color for the correct slot
- Program to delete a line given the line number from a file
- Program to Change RGB color model to HSV color model
- Slope of the line parallel to the line with the given slope
- All unique triplets that sum up to a given value
- Minimum steps to color the tree with given colors

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.