#include <bits/stdc++.h>
using namespace std;
const int N = 5e4 + 5;
int t;
long long k;
long long fac[25];
int cnt[25], m;
void init() {
  fac[0] = 1;
  for (int i = 1; i <= 20; i++) fac[i] = fac[i - 1] * i;
}
long long C(int n, int m) {
  if (n < m) return 0;
  return fac[n] / fac[m] / fac[n - m];
}
void clr() {
  memset(cnt, 0, sizeof(cnt));
  m = 0;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  init();
  cin >> t;
  while (t--) {
    clr();
    cin >> k;
    for (int i = 2; i <= 20 && k; i++) {
      cnt[k % i] += 1;
      k /= i;
      ++m;
    }
    long long res = 1;
    for (int i = m, sum = 0; i >= 0; i--) {
      if (!cnt[i]) continue;
      int x = m - i + 1 - sum - (i == 0);
      res *= C(x, cnt[i]);
      sum += cnt[i];
    }
    if (cnt[0] > 0) {
      --cnt[0];
      long long cur = 1;
      for (int i = m, sum = 0; i >= 0; i--) {
        if (!cnt[i]) continue;
        int x = m - i - sum - (i == 0);
        cur *= C(x, cnt[i]);
        sum += cnt[i];
      }
      res -= cur;
    }
    cout << res - 1 << endl;
  }
  return 0;
}
