#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T &res) {
  char c;
  T flag = 1;
  while ((c = getchar()) < '0' || c > '9')
    if (c == '-') flag = -1;
  res = c - '0';
  while ((c = getchar()) >= '0' && c <= '9') res = res * 10 + c - '0';
  res *= flag;
}
map<int, int> m;
long long C[3005][3005];
void get_C() {
  C[0][0] = 1;
  for (int i = 1; i <= 3000; i++) {
    C[i][0] = 1;
    for (int j = 1; j <= i; j++) C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
  }
}
int main() {
  get_C();
  long long x, ans;
  int t, k;
  read(t);
  while (t--) {
    k = 2;
    m.clear();
    read(x);
    while (x) {
      m[x % k]++;
      x /= k;
      k++;
    }
    k--;
    map<int, int>::iterator it = m.end();
    it--;
    ans = 1;
    int num = 0;
    while (it != m.begin()) {
      ans *= C[k - it->first - num][it->second];
      num += it->second;
      it--;
    }
    if (it->first == 0) {
      long long nans = 1;
      map<int, int>::iterator it = m.end();
      it--;
      k--;
      num = 0;
      while (it != m.begin()) {
        nans *= C[k - it->first - num][it->second];
        num += it->second;
        it--;
      }
      printf("%lld\n", ans - nans - 1);
      continue;
    }
    printf("%lld\n", ans - 1);
  }
  return 0;
}
