1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <algorithm>
#include <cassert>
#include <iostream>
#include <numeric>
#include <map>
#include <vector>
//#include "perr.hpp"

using namespace std;
const long long dzielnik = 1'000'000'007;

long long iloczyn(long long a, long long b) {
	long long res = a * b;
	res %= dzielnik;
	return res;
}

vector< long long > przygotuj_dane()
{
	int n; cin >> n;
	vector< long long > pudelka(n);
	pudelka.resize(n);

	for (int i = 0; i < n; ++i ) {
		cin >> pudelka[i];
	}

	sort(pudelka.begin(), pudelka.end(), [](const int&a, const int& b) {
		return a < b;
	});

	return pudelka;
}

void przytnij(vector< long long >& zad)
{
	long long mam = 0;

	auto it = zad.begin();
	while(it != zad.end()) {
		if(mam + 1 < *it) {
			break;
		}
		mam += *it;
		++it;
	}

	zad.erase(it, zad.end());
}

long long n_po_k_inet( long long, long long, long long);
long long n_po_k( int k, int n) {
	long long wynik = n_po_k_inet(n, k, dzielnik);
	wynik %= dzielnik;
	return wynik;
}

map< int, long long > pakuj(int zawartosc, int ile) {
	map< int, long long > pakunek;
	for(int i = 1; i <= ile; ++i) {
		pakunek[i * zawartosc] = n_po_k(i, ile);
	}
	return pakunek;
}

map< int, long long > magazynuj(map< int, long long >const& maga, map< int, long long> const& paka) {
	map< int, long long > wynik(maga);

	long long pierwszy_dosc_duzy = paka.begin()->first - 1;
	//perr << "Moge dostawiac poczawszy od " << pierwszy_dosc_duzy;

	auto it = maga.lower_bound(pierwszy_dosc_duzy);
	for( ; it != maga.end(); ++it ) {
		long long ile_mam = it->first;
		long long krotnosc = it->second;
		for(auto jt = paka.begin(); jt != paka.end(); ++jt ) {
			long long ile_mam_po_dodaniu = ile_mam + jt->first;
			long long krotnosc_po_dodaniu = jt->second;
			//perr << " do " << ile_mam << " dodaje " << jt->first << " i wychodzi " << ile_mam_po_dodaniu << " krotnosc " << krotnosc * krotnosc_po_dodaniu;
			wynik[ile_mam_po_dodaniu] += iloczyn(krotnosc, krotnosc_po_dodaniu);
			wynik[ile_mam_po_dodaniu] %= dzielnik;
		}
	}
	
	return wynik;
}

long long wylicz(vector<long long> const& zad) {
	auto it = upper_bound(zad.begin(), zad.end(), 1);
	int ile = it - zad.begin();
	//perr << "Jedynki do zapakowania " << ile;
	map< int, long long > magazyn = pakuj(1, ile);

	//perr << "Zadanie " << zad;
	//perr << "Magazyn po zapakowaniu jedynek " << magazyn;
	while( it != zad.end() ) {
		auto et = upper_bound(it, zad.end(), *it);
		int ile = et - it;
		long long co = *it;

		map< int, long long > pakunek = pakuj(co, ile);
		
		//perr << "Pudełko " << co << " x " << ile << " razy";
		//perr << pakunek;

		//perr << "Do magazynu " << magazyn << " dopakowuje " << pakunek;
		magazyn = magazynuj(magazyn, pakunek);
		//perr << "Magazyn " << magazyn;

		it = et;
	}

	return accumulate(magazyn.begin(), magazyn.end(), 0, [](long long suma, auto elem) {
		suma += static_cast< long long >(elem.second);
		suma %= dzielnik;
		return suma;
	});
}

int main() {
	vector< long long > zad = przygotuj_dane();
	przytnij(zad);
	long long wynik = wylicz(zad);

	cout << wynik << "\n";
}

long long factorial_exponent(long long n, long long p)
{
    long long ex = 0;
    do
    {
        n /= p;
        ex += n;
    } while(n > 0);
    return ex;
}

long long invert_mod(long long k, long long m)
{
    if (m == 0) return (k == 1 || k == -1) ? k : 0;
    if (m < 0) m = -m;
    k %= m;
    if (k < 0) k += m;
    int neg = 1;
    long long p1 = 1, p2 = 0, k1 = k, m1 = m, q, r, temp;
    while(k1 > 0) {
        q = m1 / k1;
        r = m1 % k1;
        temp = q*p1 + p2;
        p2 = p1;
        p1 = temp;
        m1 = k1;
        k1 = r;
        neg = !neg;
    }
    return neg ? m - p2 : p2;
}

// Preconditions: 0 <= k <= min(n,p-1); p > 1 prime
long long choose_mod_two(long long n, long long k, long long p)
{
    // reduce n modulo p
    n %= p;
    // Trivial checks
    if (n < k) return 0;
    if (k == 0 || k == n) return 1;
    // Now 0 < k < n, save a bit of work if k > n/2
    if (k > n/2) k = n-k;
    // calculate numerator and denominator modulo p
    long long num = n, den = 1;
    for(n = n-1; k > 1; --n, --k)
    {
        num = (num * n) % p;
        den = (den * k) % p;
    }
    // Invert denominator modulo p
    den = invert_mod(den,p);
    return (num * den) % p;
}

// Preconditions: 0 <= k <= n; p > 1 prime
long long choose_mod_one(long long n, long long k, long long p)
{
    // For small k, no recursion is necessary
    if (k < p) return choose_mod_two(n,k,p);
    long long q_n, r_n, q_k, r_k, choose;
    q_n = n / p;
    r_n = n % p;
    q_k = k / p;
    r_k = k % p;
    choose = choose_mod_two(r_n, r_k, p);
    // If the exponent of p in choose(n,k) isn't determined to be 0
    // before the calculation gets serious, short-cut here:
    /* if (choose == 0) return 0; */
    choose *= choose_mod_one(q_n, q_k, p);
    return choose % p;
}





long long n_po_k_inet(long long n, long long k, long long p)
{
    // We deal with the trivial cases first
    if (k < 0 || n < k) return 0;
    if (k == 0 || k == n) return 1;
    // Now check whether choose(n,k) is divisible by p
    if (factorial_exponent(n, p) > factorial_exponent(k, p) + factorial_exponent(n-k, p)) return 0;
    // If it's not divisible, do the generic work
    long long w = choose_mod_one(n,k,p);
	//perr << "!!! wynik " << w;
	return w;
}