#include <bits/stdc++.h>
using namespace std;
long long T, n, cnt, C[105], f[105], fact[25];
long long solve() {
  C[0] = f[0];
  for (long long i = 1; i <= 100; i++) C[i] = C[i - 1] + f[i];
  long long rs = 1;
  for (long long i = 2; i <= cnt + 1; i++) rs *= C[i - 1] - (i - 2);
  long long tmp = fact[C[0]];
  for (long long i = 1; i <= 100; i++) tmp *= fact[C[i] - C[i - 1]];
  rs /= tmp;
  return rs;
}
int32_t main() {
  fact[0] = 1;
  for (long long i = 1; i <= 20; i++) fact[i] = fact[i - 1] * i;
  cin >> T;
  while (T--) {
    cin >> n;
    for (long long i = 0; i <= 100; i++) f[i] = 0;
    long long p = 2;
    cnt = 0;
    while (n > 0) {
      cnt++;
      f[n % p]++;
      n /= p;
      p++;
    }
    long long rs = solve();
    if (C[0] > 0) {
      cnt--;
      f[0]--;
      cout << rs - solve() - 1 << "\n";
    } else
      cout << rs - 1 << "\n";
  }
}
