Given **N** number of people, the task is to count the number of ways to form groups of size **? N **where, in each group, the first element of the group is the leader of the group.**Note:**

- Groups with same people having different leaders are treated as a different group.
**For Example:**The group {1, 2, 3} and {2, 1, 3} are treated as different group as they have different leader 1 and 2 respectively. - Groups with same leader having same people are treated as a same group.
**For Example:**The groups {1, 3, 2} and {1, 2, 3} are treated as same group as they have same leader and same people. - The answer can be very large, take modulo to
**(1e9+7).**

**Examples:**

Input:N = 3Output:12Explanation:

Total Groups with leaders are:

Groups with Leader 1:

1. {1}

2. {1, 2}

3. {1, 3}

4. {1, 2, 3}

Groups with Leader 2:

5. {2}

6. {2, 1}

7. {2, 3}

8. {2, 1, 3}

Groups with Leader 3:

9. {3}

10. {3, 1}

11. {3, 2}

12. {3, 1, 2}

Input:N = 5Output:80

**Approach:** This problem can be solved using the concept of Binomial coefficients and modular exponentiation. Below are the observations to this problem statement:

- The number of ways to select one leader among
**N**persons is**C(N, 1)**. - For every leader we can select a group of size
**K**where**0 ≤ K ≤ N-1**to make the possible number of grouping. - So the total number ways is given by the product of
**N**and the summation of selection**K**elements from the remaining**(N – 1)**elements as:

Total Ways =

By using Binomial Theorem, the summation of the Binomial Coefficient can be written as:

Therefore the number of ways of selecting groups having only one leader is

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `long` `long` `mod = 1000000007; ` ` ` `// Function to find 2^x using ` `// modular exponentiation ` `int` `exponentMod(` `int` `A, ` `int` `B) ` `{ ` ` ` `// Base cases ` ` ` `if` `(A == 0) ` ` ` `return` `0; ` ` ` `if` `(B == 0) ` ` ` `return` `1; ` ` ` ` ` `// If B is even ` ` ` `long` `long` `y; ` ` ` `if` `(B % 2 == 0) { ` ` ` `y = exponentMod(A, B / 2); ` ` ` `y = (y * y) % mod; ` ` ` `} ` ` ` ` ` `// If B is odd ` ` ` `else` `{ ` ` ` `y = A % mod; ` ` ` `y = (y * exponentMod(A, B - 1) ` ` ` `% mod) ` ` ` `% mod; ` ` ` `} ` ` ` ` ` `return` `(` `int` `)((y + mod) % mod); ` `} ` ` ` `// Function to count the number of ` `// ways to form the group having ` `// one leader ` `void` `countWays(` `int` `N) ` `{ ` ` ` ` ` `// Find 2^(N-1) using modular ` ` ` `// exponentiation ` ` ` `long` `long` `select = exponentMod(2, ` ` ` `N - 1); ` ` ` ` ` `// Count total ways ` ` ` `long` `long` `ways ` ` ` `= ((N % mod) ` ` ` `* (select % mod)); ` ` ` ` ` `ways %= mod; ` ` ` ` ` `// Print the total ways ` ` ` `cout << ways; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `// Given N number of peoples ` ` ` `int` `N = 5; ` ` ` ` ` `// Function Call ` ` ` `countWays(N); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `import` `java.util.*; ` `class` `GFG{ ` ` ` `static` `long` `mod = ` `1000000007` `; ` ` ` `// Function to find 2^x using ` `// modular exponentiation ` `static` `int` `exponentMod(` `int` `A, ` `int` `B) ` `{ ` ` ` `// Base cases ` ` ` `if` `(A == ` `0` `) ` ` ` `return` `0` `; ` ` ` `if` `(B == ` `0` `) ` ` ` `return` `1` `; ` ` ` ` ` `// If B is even ` ` ` `long` `y; ` ` ` `if` `(B % ` `2` `== ` `0` `) ` ` ` `{ ` ` ` `y = exponentMod(A, B / ` `2` `); ` ` ` `y = (y * y) % mod; ` ` ` `} ` ` ` ` ` `// If B is odd ` ` ` `else` ` ` `{ ` ` ` `y = A % mod; ` ` ` `y = (y * exponentMod(A, B - ` `1` `) % ` ` ` `mod) % mod; ` ` ` `} ` ` ` ` ` `return` `(` `int` `)((y + mod) % mod); ` `} ` ` ` `// Function to count the number of ` `// ways to form the group having ` `// one leader ` `static` `void` `countWays(` `int` `N) ` `{ ` ` ` ` ` `// Find 2^(N-1) using modular ` ` ` `// exponentiation ` ` ` `long` `select = exponentMod(` `2` `, N - ` `1` `); ` ` ` ` ` `// Count total ways ` ` ` `long` `ways = ((N % mod) * (select % mod)); ` ` ` ` ` `ways %= mod; ` ` ` ` ` `// Print the total ways ` ` ` `System.out.print(ways); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` ` ` `// Given N number of peoples ` ` ` `int` `N = ` `5` `; ` ` ` ` ` `// Function Call ` ` ` `countWays(N); ` `} ` `} ` ` ` `// This code is contributed by sapnasingh4991 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach ` `mod ` `=` `1000000007` ` ` `# Function to find 2^x using ` `# modular exponentiation ` `def` `exponentMod(A, B): ` ` ` ` ` `# Base cases ` ` ` `if` `(A ` `=` `=` `0` `): ` ` ` `return` `0` `; ` ` ` `if` `(B ` `=` `=` `0` `): ` ` ` `return` `1` `; ` ` ` ` ` `# If B is even ` ` ` `y ` `=` `0` `; ` ` ` ` ` `if` `(B ` `%` `2` `=` `=` `0` `): ` ` ` `y ` `=` `exponentMod(A, B ` `/` `/` `2` `); ` ` ` `y ` `=` `(y ` `*` `y) ` `%` `mod; ` ` ` ` ` `# If B is odd ` ` ` `else` `: ` ` ` `y ` `=` `A ` `%` `mod; ` ` ` `y ` `=` `(y ` `*` `exponentMod(A, B ` `-` `1` `) ` `%` ` ` `mod) ` `%` `mod; ` ` ` ` ` `return` `((y ` `+` `mod) ` `%` `mod); ` ` ` `# Function to count the number of ` `# ways to form the group having ` `# one leader ` `def` `countWays(N): ` ` ` ` ` `# Find 2^(N-1) using modular ` ` ` `# exponentiation ` ` ` `select ` `=` `exponentMod(` `2` `, N ` `-` `1` `); ` ` ` ` ` `# Count total ways ` ` ` `ways ` `=` `((N ` `%` `mod) ` `*` `(select ` `%` `mod)); ` ` ` ` ` `ways ` `%` `=` `mod; ` ` ` ` ` `# Print the total ways ` ` ` `print` `(ways) ` ` ` `# Driver code ` `if` `__name__` `=` `=` `'__main__'` `: ` ` ` ` ` `# Given N number of people ` ` ` `N ` `=` `5` `; ` ` ` ` ` `# Function call ` ` ` `countWays(N); ` ` ` `# This code is contributed by rutvik_56 ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System; ` `class` `GFG{ ` ` ` `static` `long` `mod = 1000000007; ` ` ` `// Function to find 2^x using ` `// modular exponentiation ` `static` `int` `exponentMod(` `int` `A, ` `int` `B) ` `{ ` ` ` `// Base cases ` ` ` `if` `(A == 0) ` ` ` `return` `0; ` ` ` `if` `(B == 0) ` ` ` `return` `1; ` ` ` ` ` `// If B is even ` ` ` `long` `y; ` ` ` `if` `(B % 2 == 0) ` ` ` `{ ` ` ` `y = exponentMod(A, B / 2); ` ` ` `y = (y * y) % mod; ` ` ` `} ` ` ` ` ` `// If B is odd ` ` ` `else` ` ` `{ ` ` ` `y = A % mod; ` ` ` `y = (y * exponentMod(A, B - 1) % ` ` ` `mod) % mod; ` ` ` `} ` ` ` ` ` `return` `(` `int` `)((y + mod) % mod); ` `} ` ` ` `// Function to count the number of ` `// ways to form the group having ` `// one leader ` `static` `void` `countWays(` `int` `N) ` `{ ` ` ` ` ` `// Find 2^(N-1) using modular ` ` ` `// exponentiation ` ` ` `long` `select` `= exponentMod(2, N - 1); ` ` ` ` ` `// Count total ways ` ` ` `long` `ways = ((N % mod) * (` `select` `% mod)); ` ` ` ` ` `ways %= mod; ` ` ` ` ` `// Print the total ways ` ` ` `Console.Write(ways); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` ` ` `// Given N number of peoples ` ` ` `int` `N = 5; ` ` ` ` ` `// Function Call ` ` ` `countWays(N); ` `} ` `} ` ` ` `// This code is contributed by sapnasingh4991` |

*chevron_right*

*filter_none*

**Output:**

80

**Time Complexity:** O(log N) **Auxiliary Space:** O(1)

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:

- Count of ways to distribute N items among 3 people with one person receiving maximum
- Count ways to distribute m items among n people
- Distribute N candies among K people
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Count of numbers having only one unset bit in a range [L,R]
- Number of ways of distributing N identical objects in R distinct groups with no groups empty
- Ways of dividing a group into two halves such that two elements are in different groups
- Ways to form a group from three groups with given constraints
- Count of groups having largest size while grouping according to sum of its digits
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Find the count of numbers that can be formed using digits 3, 4 only and having length at max N.
- Count of numbers having only 1 set bit in the range [0, n]
- Count number of binary strings of length N having only 0's and 1's
- Minimize the sum of the squares of the sum of elements of each group the array is divided into
- Count of ways to make Array sum even by removing only one element
- Count sequences of length K having each term divisible by its preceding term
- Count of permutations of an Array having each element as a multiple or a factor of its index
- Find if two people ever meet after same number of jumps
- Check if two people starting from different points ever meet

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.