public int derange(int n) {
    if (values[n] != 0)
      return values[n];
    if (n == 1)
      return values[1] = 0;
    if (n == 2)
      return values[2] = 1;
    return values[n] = (n - 1) * (derange(n - 1) + derange(n - 2)) % 1000000007;
  }