A number K$K$ is said to be magical if it can be represented as a power of 2 only.That is K$K$=2x$2^{x}$ for some natural number x$x$. 
Given a string of digits S$S$ of length N$N$, Let P be a valid arrangement of S.
By valid arrangement we mean that it should not have any leading zeroes.
Find the sum of all such distinct Pi's, which, when treated as a number is magical.
Two arrangements are said to be distinct, if there is atleast one index where they differ. 
As the sum can be very large, print the sum modulo 109+7$10^{9}+7$.

-----Input:-----
-The first line of the input contains a single integer T$T$ denoting the number of test cases. 
-Only line of each test case contains a string S$S$ of length N$N$, consisting only of digits between 0 to 9.

-----Output:-----
For each test case, print a single integer denoting the sum of all such magical Pi′s$Pi's$ modulo 109+7$10^{9}+7$.
If no such Pi$Pi$ exists print "-1".

-----Constraints-----
- 1≤T≤1000$1 \leq T \leq 1000$
- 2≤N≤1000$2 \leq N \leq 1000$
- String only consists of digits between 0 to 9, both inclusive.

-----Subtasks-----
- 10 points : 1≤N≤5$1 \leq N \leq 5$
- 40 points : 1≤N≤100$1 \leq N \leq 100$
- 50 points : Original Constraints

-----Sample Input:-----
2
35566

31

-----Sample Output:-----
65536

-1

-----EXPLANATION:-----
Only arrangement is 65536.
No arrangement of 31 gives us a power of two.

Source: codechef | Difficulty: UNKNOWN_DIFFICULTY


Solutions:
from math import log2, ceil
MOD = int(1000000000.0 + 7)
srt = lambda s: ''.join(sorted(s))
for _ in range(int(input())):
	s = srt(input())
	res = -1
	for p in range(ceil(log2(int(s))), int(log2(int(s[::-1]))) + 1):
		if int(srt(str(pow(2, p)))) == int(s):
			if res == -1:
				res = 0
			res = (res + pow(2, p, MOD)) % MOD
	print(res)


from collections import Counter
l = [pow(2, i) for i in range(3321)]
d = {i: Counter(str(i)) for i in l}
t = int(input())
while t:
	x = int(input())
	n = len(str(x))
	dx = Counter(str(x))
	ans = 0
	for i in l:
		if len(str(i)) == n:
			if dx == d[i]:
				ans += i
		elif len(str(i)) > n:
			break
	if ans == 0:
		print(-1)
	else:
		print(ans % 1000000007)
	t -= 1


from math import log, floor
t = int(input())
while t > 0:
	n = str(input())
	n1 = int(''.join(sorted(n)))
	n2 = int(''.join(sorted(n, reverse=True)))
	low = pow(2, floor(log(n1, 2)))
	high = pow(2, floor(log(n2, 2)))
	i = low
	ans = 0
	while i <= high:
		k = str(i)
		k = ''.join(sorted(k))
		if i >= n1 and int(k) == n1:
			ans += i
		i = i << 1
	mod = 1000000007
	if ans != 0:
		print(ans % mod)
	else:
		print(-1)
	t -= 1