#include <bits/stdc++.h>
using namespace std;
long long n;
int cnt[100] = {0};
long long fact(int n) {
  if (n == 0) return 1;
  return n * fact(n - 1);
}
void solve() {
  memset(cnt, 0, sizeof(cnt));
  cin >> n;
  int dv = 2;
  int i = 0;
  while (n > 0) {
    cnt[n % dv]++;
    n /= dv++;
  }
  dv--;
  long long otpt1 = 1;
  long long div1 = fact(cnt[0]);
  long long currp = cnt[0];
  for (int i = 2; i <= dv; i++) {
    currp += cnt[i - 1];
    otpt1 *= (currp - i + 2);
    div1 *= fact(cnt[i - 1]);
  }
  otpt1 /= div1;
  long long otpt2 = 0;
  long long div2 = 1;
  if (cnt[0] > 0) {
    otpt2 = 1;
    long long div2 = fact(cnt[0] - 1);
    currp = cnt[0] - 1;
    for (int i = 2; i <= dv - 1; i++) {
      currp += cnt[i - 1];
      otpt2 *= (currp - i + 2);
      div2 *= fact(cnt[i - 1]);
    }
    otpt2 /= div2;
  }
  cout << otpt1 - otpt2 - 1 << endl;
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int tc = 1;
  cin >> tc;
  for (int i = 1; i <= tc; i++) {
    solve();
  }
  return 0;
}
