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
/*
 *  Copyright (C) 2020  Paweł Widera
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details:
 *  http://www.gnu.org/licenses/gpl.html
 */
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
#include <array>
#include <deque>
using namespace std;


#ifdef DEBUG
	template<typename... ArgTypes>
	inline void debug_print(ArgTypes... args) {
		using expand = int[];
		(void)expand{0, ((cerr << args << " "), void(), 0)... };
		cerr << endl;
	}

	#define DBG(...) debug_print(__VA_ARGS__);
	#define DBGx(x) cerr << #x << " " << x << endl;
	#define DBGa(array) cerr << #array << " "; for (auto a: array) { cerr << a << " "; } cerr << endl;
#else
	#define DBG(...)
	#define DBGx(x)
	#define DBGa(array)
#endif

#define MAX_SIZE 5001
#define MOD 1000000007


struct Box {
	int size;
	int count;

	friend ostream& operator<<(ostream& os, const Box& b) {
		if (b.count > 1) {
			return os << b.size << "x" << b.count;
		}
		return os << b.size;
	}
};


struct State {
	int index;
	long long int sum;
	long long int multiplier;
};


int binomial(int k, int n) {
	if (k == n) return 1;
	if (k == 1) return n;

	k = min(k, n - k);
	long long int count = 1;
    for (int i = 0; i < k; ++i) {
        count = (count * (n - i) / (i + 1)) % MOD;
	}
	return count;
}


int power2(int n) {
	if (n == 0) return 1;

    int x = power2(n / 2);
    if (n % 2 == 0) return (x * x) % MOD;
    return (2 * x * x) % MOD;
}


int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int n, size;
	array<int, MAX_SIZE> bucket = {0};
	vector<Box> boxes;

	cin >> n;
	boxes.reserve(n);

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

	for (int i = 0; i < MAX_SIZE; ++i) {
		if (bucket[i]) {
			boxes.push_back({i, bucket[i]});
		}
	}

	long long int count = 0;
	deque<State> queue;
	queue.push_back({0, 0, 1});
	int MAX_INDEX = boxes.size() - 1;

	while (!queue.empty()) {
		auto state = queue.front();
		queue.pop_front();
		auto current = boxes[state.index];

DBG("---\n", current)
		// continuity gap, stop the count
		if (current.size > state.sum + 1) {
			continue;
		}

		// check if continuity holds when the current box is skipped
		for (int i = state.index + 1; i <= MAX_INDEX; ++i) {
			if (boxes[i].size > state.sum + 1) {
				break;
			}
			queue.push_back({i, state.sum, state.multiplier});
		}

		// single value
		if (current.count == 1) {
			count = (count + state.multiplier) % MOD;
DBG("single, sum =", state.sum, "\ncounter:", count, " increase:", state.multiplier)
			if (state.index < MAX_INDEX) {
				queue.push_back({state.index + 1, state.sum + current.size, state.multiplier});
			}
		}
		// multiples, add all non-empty subsets
		else {
			count = (count + state.multiplier * (power2(current.count) - 1)) % MOD;
DBG("multi, sum =", state.sum, "m =", state.multiplier, "*", (power2(current.count) - 1),
	"\ncounter:", count, " increased:", state.multiplier * (power2(current.count) - 1))
			if (state.index < MAX_INDEX) {
				for (int k = 1; k <= current.count; ++k) {
					int m = (state.multiplier * binomial(k, current.count)) % MOD;
					queue.push_back({state.index + 1, state.sum + k * current.size, m});
				}
			}
		}
	}

	cout << count << endl;
	return 0;
}