Count N | Function to calculate the power of n ^ k % p ; Update x if it is more than or equal to p ; In case x is divisible by p ; ; If y is odd , multiply x with result ; y must be even now y = y >> 1 y = y / 2 ; Function to count the number of arrays satisfying required conditions ; Calculating N ^ K ; Driver Code
def power ( x , y , p ) : NEW_LINE INDENT res = 1 NEW_LINE x = x % p NEW_LINE if ( x == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = ( res * x ) % p NEW_LINE DEDENT x = ( x * x ) % p NEW_LINE DEDENT return res NEW_LINE DEDENT def countArrays ( n , k ) : NEW_LINE INDENT mod = 1000000007 NEW_LINE ans = power ( n , k , mod ) NEW_LINE return ans NEW_LINE DEDENT n = 3 NEW_LINE k = 5 NEW_LINE ans = countArrays ( n , k ) NEW_LINE print ( ans ) NEW_LINE