Find the sum of power of bit count raised to the power B | Function to calculate a ^ b mod m using fast - exponentiation method ; Function to calculate sum ; Itereate for all values of array A ; Calling fast - exponentiation and appending ans to sum ; Driver code .
def fastmod ( base , exp , mod ) : NEW_LINE INDENT if ( exp == 0 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT elif ( exp % 2 == 0 ) : NEW_LINE INDENT ans = fastmod ( base , exp / 2 , mod ) ; NEW_LINE return ( ans % mod * ans % mod ) % mod ; NEW_LINE DEDENT else : NEW_LINE INDENT return ( fastmod ( base , exp - 1 , mod ) % mod * base % mod ) % mod ; NEW_LINE DEDENT DEDENT def findPowerSum ( n , ar ) : NEW_LINE INDENT mod = int ( 1e9 ) + 7 ; NEW_LINE sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT base = bin ( ar [ i ] ) . count ( '1' ) ; NEW_LINE exp = ar [ i ] ; NEW_LINE sum += fastmod ( base , exp , mod ) ; NEW_LINE sum %= mod ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 ; NEW_LINE ar = [ 1 , 2 , 3 ] ; NEW_LINE print ( findPowerSum ( n , ar ) ) ; NEW_LINE DEDENT