#include <bits/stdc++.h>
using namespace std;
inline void prep() {
  cin.tie(0);
  cin.sync_with_stdio(0);
};
const double pi = acos(-1.);
long long mod = 998244353;
long long mymod(long long num) {
  if (num < 0 && !(num % mod == 0)) {
    if (mod == 1) {
      return 0;
    }
    return mod - abs(num) % mod;
  } else {
    return num % mod;
  }
}
const int biggg = 2000000000;
const long long bigggest = 1000000000000000000LL;
const double eps = .0000000001;
long long choose(long long n, long long k) {
  if (k == 0) return 1;
  return (n * choose(n - 1, k - 1)) / k;
}
int main() {
  prep();
  int t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long res = 1;
    long long zerofront = 1;
    vector<int> nums;
    int cnt = 2;
    while (n > 0) {
      nums.push_back(n % cnt);
      n /= cnt;
      cnt++;
    }
    sort(nums.rbegin(), nums.rend());
    int i = 0;
    while (i < nums.size()) {
      int b4 = nums[i];
      int b4ind = i;
      while (i < nums.size() && nums[i] == b4) i++;
      res *= choose(nums.size() - max(0, b4 - 1) - b4ind, i - b4ind);
      zerofront *= choose(nums.size() - max(0, b4 - 1) - b4ind - 1,
                          i - b4ind - (b4 == 0));
    }
    cout << res - 1 - zerofront << '\n';
  }
  return 0;
}
