Under construction

Still being built. Things may change

Key Storage

Time limit3sMemory limit512 MB

Summary
For each key, count how many other positive integers produce the same multiset of remainders when repeatedly divided by 2, 3, 4, and so on.
Level

Hard8 of 10

Topics
Combinatorics, Math, Dynamic programming, Number theory
Solved
No attempts yet

Problem

Karl is developing a key storage service. Each user has one positive integer key.

Karl knows that storing keys in plain text is bad practice. So instead of storing a key, he decided to store a fingerprint of a key. Using some existing fingerprint algorithm looked too boring to him, so he invented his own.

Karl's fingerprint is calculated by the following process: divide the given integer by 2, then divide the result by 3, then divide the result by 4, and so on, until the result equals zero. Each division is integer division. The fingerprint is the multiset of the remainders of these divisions.

For example, this is how Karl's fingerprint algorithm is applied to the key 11: 11 divided by 2 has remainder 1 and result 5, then 5 divided by 3 has remainder 2 and result 1, and 1 divided by 4 has remainder 1 and result 0. Thus, the key 11 produces the sequence of remainders [1,2,1][1, 2, 1] and has the fingerprint multiset {1,1,2}\{1, 1, 2\}.

Ksenia wants to prove that Karl's fingerprint algorithm is not very good. For example, she found that both keys 178800 and 123456 produce the fingerprint {0,0,0,0,2,3,3,4}\{0, 0, 0, 0, 2, 3, 3, 4\}. Thus, users are at risk of fingerprint collision with some commonly used and easy to guess keys like 123456.

Ksenia wants to make her words more persuasive. She wants to calculate the number of other keys that have the same fingerprint as each key in the given list of some commonly used keys. Your task is to help her.

Input

The first line contains an integer tt (1≤t≤50 0001 \le t \le 50\,000), the number of commonly used keys to examine. Each of the next tt lines contains one integer kik_i (1≤ki≤10181 \le k_i \le 10^{18}), the key itself.

Output

For each of the keys print one integer: the number of other keys that have the same fingerprint.

Hint

The other key with the same fingerprint as 11 is 15. 15 produces the sequence of remainders [1,1,2][1, 1, 2]. So both numbers have the fingerprint multiset {1,1,2}\{1, 1, 2\}.

Examples1

  1. Example 1

    Input
    3
    1
    11
    123456
    
    Expected output
    0
    1
    127