#include <bits/stdc++.h>
using namespace std;
const int MAXN = (int)1e5 + 3;
const int MOD = (int)1e9 + 7;
int add(int a, int b) {
  int c = a + b;
  if (c >= MOD) c -= MOD;
  if (c < 0) c += MOD;
  return c;
}
int mul(int a, int b) {
  int c = 1LL * a * b % MOD;
  return c;
}
int pwr(int a, int b) {
  if (!b) return 1;
  int res = pwr(a, b >> 1);
  res = mul(res, res);
  if (b & 1) res = mul(res, a);
  return res;
}
int inv(int p) { return pwr(p, MOD - 2); }
long long fact[MAXN], invfact[MAXN], n, a, part[21], fp[20], cnt, ans = 1,
                                                                  notans;
int C(int n, int k) {
  int ans = fact[n];
  ans = mul(ans, invfact[k]);
  ans = mul(ans, invfact[n - k]);
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  fact[0] = 1;
  for (int i = 1; i < MAXN; i++) fact[i] = mul(fact[i - 1], i);
  for (int i = 0; i < MAXN; i++) invfact[i] = inv(fact[i]);
  cin >> n;
  for (int i = 0; i < n; i++) {
    notans = 0;
    ans = 1;
    for (int i = 0; i < 20; i++) fp[i] = 0;
    cin >> a;
    for (int i = 2; a > 0; i++) {
      fp[a % i]++;
      a /= i;
      cnt = i;
    }
    part[2] = fp[0] + fp[1];
    for (int i = 3; i <= cnt; i++) {
      part[i] = part[i - 1] + fp[i - 1] - 1;
    }
    for (int i = 2; i <= cnt; i++) {
      ans *= part[i];
    }
    if (fp[0]) {
      notans = 1;
      for (int i = 2; i <= cnt; i++) {
        if (i != cnt) notans *= (part[i] - 1);
      }
      for (int i = 0; i < 20; i++) {
        if (i == 0)
          notans /= fact[fp[i] - 1];
        else
          notans /= fact[fp[i]];
      }
    }
    for (int i = 0; i < 20; i++) {
      ans /= fact[fp[i]];
    }
    cout << ans - notans - 1 << endl;
  }
}
