#include <bits/stdc++.h>
using namespace std;
using namespace std;
const int SI = 2e5 + 100;
const long long mod = 998244353;
long long k;
int a[22];
long long C(long long n, long long m) {
  long long res = 1;
  for (long long i = 0; i < m; i++) res = res * (n - i) / (i + 1);
  return res;
}
long long solve(long long num) {
  long long res = 1;
  long long used = 0;
  for (int i = 20; i >= 0; i--) {
    if (a[i] > 0) {
      long long have = num - i + 1 - used;
      if (i == 0) have--;
      res *= C(have, a[i]);
      used += a[i];
    }
  }
  return res;
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    memset(a, 0, sizeof(a));
    cin >> k;
    long long p = 2;
    int num = 0;
    while (k) {
      a[k % p]++;
      k /= p;
      p++;
      num++;
    }
    long long ans = solve(num);
    if (a[0]) {
      a[0]--;
      ans -= solve(num - 1);
    }
    cout << ans - 1 << endl;
  }
  return 0;
}
