Function to calculate 2 ^ N % mod
def mod_pow2 ( n ) : NEW_LINE INDENT while n >= len ( MEM ) : NEW_LINE INDENT MEM . append ( ( MEM [ - 1 ] * 2 ) % MOD ) NEW_LINE DEDENT return MEM [ n ] NEW_LINE DEDENT