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