#include <bits/stdc++.h>
using namespace std;
unsigned long long p[22];
unsigned long long c(int n, int k) {
  if (n < k) return 0;
  return p[n] / (p[n - k] * p[k]);
}
int t, a[22], b[22];
unsigned long long x, d[22][2];
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  p[0] = 1;
  for (int i = 1; i <= 20; ++i) {
    p[i] = p[i - 1] * i;
  }
  cin >> t;
  while (t--) {
    cin >> x;
    for (int i = 0; i <= 20; ++i) {
      b[i] = 0;
      a[i] = 0;
      d[i][0] = d[i][1] = 0;
    }
    b[0] = -1;
    for (int j = 2; j <= 20 && x > 0; ++j) {
      ++a[x % j];
      for (int i = 0; i < j; ++i) ++b[i];
      x /= j;
    }
    int cnt = 0;
    int i = 20;
    while (!b[i]) --i;
    d[i + 1][0] = 1;
    for (; i >= 1; --i) {
      d[i][0] = d[i + 1][0] * c(b[i] - cnt - 1, a[i]);
      d[i][1] = d[i + 1][1] * c(b[i] - cnt, a[i]);
      if (a[i]) d[i][1] += d[i + 1][0] * c(b[i] - cnt - 1, a[i] - 1);
      cnt += a[i];
    }
    cout << d[1][1] - 1 << endl;
  }
  return 0;
}
