#include #include using namespace std; using namespace atcoder; using mint = modint1000000007; vector memo(100001, vector(30, vector(30))); vector fact, inv; void init(int n) { fact.push_back(1); inv.push_back(1); for (int i = 0; i < n; i++) { fact.push_back(fact.back() * (i + 1)); inv.push_back(1 / fact.back()); } } mint ncr(int n, int r) { if (r < 0 or n - r < 0) { return 0; } return fact[n] * inv[r] * inv[n - r]; } mint calc(int n, int pc0, int pc) { if (n == 0) { return pc0 == pc; } mint res = 0; for (int i = pc0; i <= pc; i++) { res += calc(n - 1, i, pc) * ((1 << pc0) * ncr(pc - pc0, i - pc0) - (i == pc0)); } memo.at(n).at(pc0).at(pc) = res; return res; } int main(int argc, char const *argv[]) { int n; cin >> n; vector b(n + 1); for (int i = 0; i < n; i++) { cin >> b.at(i + 1); } init(30); mint ans = 1; int last = 0; for (int i = 0; i < n; i++) { if (b.at(i + 1) != -1) { if ((b.at(last) & b.at(i + 1)) != b.at(last)) { ans = 0; break; } ans *= calc(i + 1 - last, __builtin_popcount(b.at(last)), __builtin_popcount(b.at(i + 1))); last = i + 1; } } cout << ans.val() << endl; return 0; }