malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   struct data1 {     long long sum, index, position;   };   bool comp(data1 a, data1 b) { return a.sum < b.sum; }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(0);     long long k;     cin >> k;     vector<data1> arr;     long long sz = 0;     for (long long i = 0; i < k; i++) {       long long n;       cin >> n;       std::vector<long long> v(n);       for (auto &x : v) cin >> x;       long long sum = accumulate(v.begin(), v.end(), 0ll);       for (long long j = 0; j < n; j++) {         data1 d;         d.sum = sum - v[j];         d.index = i;         d.position = j;         arr.push_back(d);         sz++;       }     }     sort(arr.begin(), arr.end(), &comp);     bool flag = false;     for (long long i = 0; i < sz - 1; i++) {       if (arr[i].sum == arr[i + 1].sum and arr[i].index != arr[i + 1].index) {         cout <<  YES n ;         cout << arr[i].index + 1 <<     << arr[i].position + 1 <<   n ;         cout << arr[i + 1].index + 1 <<     << arr[i + 1].position + 1 <<   n ;         flag = true;         break;       }     }     if (flag) return 0;     cout <<  NO n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool f[200000] = {0};   string ans, s;   multiset<int> g[66000];   int n, in[66000] = {0}, out[66000] = {0}, kol1 = 0, kol2 = 0, start = 0;   void dfs(int v) {     while (!g[v].empty()) {       int tmp = *g[v].begin();       g[v].erase(g[v].begin());       dfs(tmp);     }     ans += char(v % 255);   }   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> s;       int v = int(s[0]) * 255 + int(s[1]);       int u = int(s[1] * 255) + int(s[2]);       start = v;       g[v].insert(u);       out[v]++;       in[u]++;     }     for (int i = 1; i <= 65025; i++) {       int d = out[i] - in[i];       if (d == 1) {         kol1++;         start = i;       } else {         if (d == -1)           kol2++;         else if (d != 0) {           cout <<  NO ;           return 0;         }       }       if (kol1 > 1 || kol2 > 1) {         cout <<  NO ;         return 0;       }     }     dfs(start);     ans += char(start / 255);     reverse(ans.begin(), ans.end());     if (ans.length() == n + 2) {       cout <<  YES             <<   n ;       cout << ans;     } else       cout <<  NO ;   }
#include <bits/stdc++.h>   using namespace std;   void solve() {     string s, a =   , b;     cin >> s;     unordered_map<string, int> m;     m[s]++;     for (int i = s.length() - 1; i >= 0; i--) {       a += s[i];       b = a;       reverse(b.begin(), b.end());       m[b + s.substr(0, i)]++;     }     cout << m.size() << endl;   }   int main() {     { solve(); }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   int main() {     cin >> n;     int k = 0;     if (n == 1 || n == 2)       cout << -1;     else if (n == 3)       cout << 210;     else       for (int i = 4; i <= n; i++) {         k++;         if (k == 7) k = 1;       }     if (k == 1) {       cout << 1;       for (int i = 2; i <= n - 2; i++) cout << 0;       cout << 50;     } else if (k == 2) {       cout << 1;       for (int i = 2; i <= n - 2; i++) cout << 0;       cout << 80;     } else if (k == 3) {       cout << 1;       for (int i = 2; i <= n - 3; i++) cout << 0;       cout << 170;     } else if (k == 4) {       cout << 1;       for (int i = 2; i <= n - 2; i++) cout << 0;       cout << 20;     } else if (k == 5) {       cout << 1;       for (int i = 2; i <= n - 3; i++) cout << 0;       cout << 200;     } else if (k == 6) {       cout << 1;       for (int i = 2; i <= n - 3; i++) cout << 0;       cout << 110;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 1e5 + 5;   int minn[MAX], maxx[MAX], h[MAX];   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; ++i) cin >> h[i];     int block = 1;     minn[1] = h[1];     maxx[1] = h[1];     for (int i = 2; i <= n; ++i) {       if (maxx[block] <= h[i]) {         block++;         minn[block] = h[i];         maxx[block] = h[i];       } else {         minn[block] = min(minn[block], h[i]);         maxx[block] = max(maxx[block], h[i]);         while (block > 1 && maxx[block - 1] > minn[block]) {           --block;           minn[block] = min(minn[block], minn[block + 1]);           maxx[block] = max(maxx[block], maxx[block + 1]);         }       }     }     cout << block;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 100005, SQRT = 550, Q = 100005;   struct Query {     int l, r, id;     Query() {}     Query(int l, int r, int id) : l(l), r(r), id(id) {}     bool operator<(const Query& q) const {       if (l / SQRT != q.l / SQRT) return l / SQRT < q.l / SQRT;       return r > q.r;     }   } queries[Q];   int arr[N], n, k;   int freq[2000005], answer[Q], mo_distinct, mo_has_special;   deque<int> nums[N];   map<int, int> freq_ratio[N];   inline void add_elem(int x) {     if (++freq[x] == 1) ++mo_distinct;   }   inline void rem_elem(int x) {     if (freq[x]-- == 1) --mo_distinct;   }   inline void add_freq_ratio(int x, int diff) {     bool before = freq_ratio[x].size() <= 1;     ++freq_ratio[x][diff];     bool after = freq_ratio[x].size() == 1;     if (!before && after) ++mo_has_special;     if (before && !after) --mo_has_special;   }   inline void rem_freq_ratio(int x, int diff) {     bool before = freq_ratio[x].size() == 1;     --freq_ratio[x][diff];     if (freq_ratio[x][diff] == 0) freq_ratio[x].erase(diff);     bool after = freq_ratio[x].size() <= 1;     if (!before && after) ++mo_has_special;     if (before && !after) --mo_has_special;   }   void add_begin(int idx, int x) {     add_elem(x);     if (!nums[x].empty()) {       int diff = nums[x].front() - idx;       add_freq_ratio(x, diff);     } else {       ++mo_has_special;     }     nums[x].push_front(idx);   }   void add_end(int idx, int x) {     add_elem(x);     if (!nums[x].empty()) {       int diff = idx - nums[x].back();       add_freq_ratio(x, diff);     } else {       ++mo_has_special;     }     nums[x].push_back(idx);   }   void rem_begin(int x) {     rem_elem(x);     int front = nums[x].front();     nums[x].pop_front();     if (!nums[x].empty()) {       int diff = nums[x].front() - front;       rem_freq_ratio(x, diff);     } else {       --mo_has_special;     }   }   void rem_end(int x) {     rem_elem(x);     int back = nums[x].back();     nums[x].pop_back();     if (!nums[x].empty()) {       int diff = back - nums[x].back();       rem_freq_ratio(x, diff);     } else {       --mo_has_special;     }   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; ++i) scanf( %d , &arr[i]);     int q;     scanf( %d , &q);     for (int i = 0; i < q; ++i) {       int l, r;       scanf( %d %d , &l, &r);       --l;       --r;       queries[i] = Query(l, r, i);     }     sort(queries, queries + q);     mo_has_special = mo_distinct = 0;     int l = 0, r = 0;     add_end(0, arr[0]);     for (int i = 0; i < q; ++i) {       while (r < queries[i].r) {         ++r;         add_end(r, arr[r]);       }       while (l > queries[i].l) {         --l;         add_begin(l, arr[l]);       }       while (l < queries[i].l) rem_begin(arr[l++]);       while (r > queries[i].r) rem_end(arr[r--]);       if (mo_has_special != 0) {         answer[queries[i].id] = mo_distinct;       } else {         answer[queries[i].id] = mo_distinct + 1;       }     }     for (int i = 0; i < q; ++i) printf( %d n , answer[i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     ;     vector<int> v(n);     long long x = 0;     for (int i = 0; i < n; i++) {       char x;       cin >> x;       if ((x -  0 ) % 2 == 0) {         v.push_back(i + 1);       }     }     for (int i = 0; i < v.size(); i++) {       x += v[i];     }     cout << x << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 5e5 + 10;   int P[N], ar[N];   long long c[N];   vector<int> v[N];   bool used[N];   void init() {     P[1] = 1;     int i, j;     for (i = 1; i < N; ++i)       for (j = (i * 2); j < N; j += i) P[j] -= P[i];   }   int main() {     int tt, q, x, n, i, j;     scanf( %d%d , &n, &q);     long long ret = 0;     init();     for (i = 1; i <= n; ++i) {       scanf( %d , &ar[i]);       x = ar[i];       for (j = 1; j * j <= x; ++j)         if (x % j == 0) {           v[i].push_back(j);           if ((x / j) != j) v[i].push_back(x / j);         }     }     for (i = 0; i < q; ++i) {       scanf( %d , &x);       for (j = 0; j < v[x].size(); ++j) {         tt = v[x][j];         ret -= c[tt] * (c[tt] - 1) / 2 * P[tt];         if (used[x])           --c[tt];         else           ++c[tt];         ret += c[tt] * (c[tt] - 1) / 2 * P[tt];       }       used[x] ^= true;       cout << ret << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   void debug() {}   int const N = 1e3 + 9, OO = 1e9;   int main() {     int n, q, type, a;     char b;     bool prd = 0;     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       for (int j = 0; j < n; ++j) {         scanf(  %c , &b);         if (j == i && b ==  1 ) prd = !prd;       }     }     scanf( %d , &q);     for (int i = 0; i < q; ++i) {       scanf( %d , &type);       if (type != 3) scanf( %d , &a);       if (type == 1 || type == 2)         prd = !prd;       else         printf((prd) ?  1  :  0 );     }   }
#include <bits/stdc++.h>   using namespace std;   const int maxN = (int)1e6 + 10;   int mod = (int)1e9 + 7;   int sum(int a, int b) {     int s = a + b;     if (s >= mod) s -= mod;     return s;   }   int n;   int mult(int a, int b) { return (1LL * a * b) % mod; }   int a[maxN];   int inv[maxN];   int fact;   void calc() {     inv[1] = 1;     for (int i = 2; i < maxN; i++) {       inv[i] = mult((mod - (mod / i)), inv[mod % i]);     }     fact = 1;     for (int i = 2; i <= n; i++) fact = mult(fact, i);   }   int main() {     ios_base::sync_with_stdio(false);     srand(time(0));     srand(time(0));     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     calc();     sort(a + 1, a + n + 1);     int mx = a[n];     int ans = 0;     int x = 0;     int prv = 0;     int sz = 1;     for (int i = 1; i <= n; i++) {       if (a[i] == mx) break;       if (i > 1) {         if (a[i] == a[i - 1])           sz++;         else {           prv = prv + sz;           sz = 1;         }       }       ans = sum(ans, mult(mult(a[i], fact), inv[n - prv]));     }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using lint = long long;   int main(void) {     lint Q, N;     cin >> Q;     vector<lint> ans(Q);     for (int i = 0; i < Q; i++) {       cin >> N;       lint sum = 0;       for (int k = 0; k < N; k++) {         int x;         cin >> x;         sum += x;       }       ans[i] = ceil(sum / (double)N);     }     for (int i = 0; i < Q; i++) cout << ans[i] << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const int MAX_N = 1e3 + 9;   int par[MAX_N], rankv[MAX_N];   vector<pair<int, int>> cycles;   vector<array<int, 4>> ans;   void init(int n) {     for (int i = 1; i <= n; i++) par[i] = i, rankv[i] = 1;   }   int find_set(int u) {     if (par[u] == u) return u;     return par[u] = find_set(par[u]);   }   void link(int x, int y) {     if (rankv[x] > rankv[y]) swap(x, y);     par[x] = y;     rankv[y] += (rankv[y] == rankv[x]);   }   bool union_set(int x, int y) {     x = find_set(x), y = find_set(y);     if (x != y) link(x, y);     return x != y;   }   int32_t main() {     ios_base::sync_with_stdio(0), cin.tie(0);     int n, u, v;     cin >> n;     init(n);     for (int i = 0; i < n - 1; i++) {       cin >> u >> v;       if (!union_set(u, v)) cycles.push_back({u, v});     }     for (int i = 0; i < cycles.size(); i++) {       auto cur = cycles[i];       for (int j = 1; j <= n; j++) {         if (union_set(cur.first, j)) {           ans.push_back({cur.first, cur.second, cur.first, j});           break;         }       }     }     if (ans.empty()) cout << 0, exit(0);     cout << ans.size() <<   n ;     for (auto i : ans) {       for (int j = 0; j < 4; j++) cout << i[j] <<    [j == 3];       cout <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   int N;   long long arr[100020];   long long prefix[100020];   multiset<long long> s;   set<int> ded;   long long range(int i, int j) {     if (i == 0) {       return prefix[j];     } else {       return prefix[j] - prefix[i - 1];     }   }   int32_t main() {     ios_base::sync_with_stdio(false);     if (fopen( cf722c.in ,  r )) {       freopen( cf722c.in ,  r , stdin);       freopen( cf722c.out ,  w , stdout);     }     cin >> N;     arr[0] = 0;     for (int i = 1; i <= N; i++) {       cin >> arr[i];     }     prefix[0] = arr[0];     for (int i = 1; i <= N + 1; i++) {       prefix[i] = prefix[i - 1] + arr[i];     }     s.insert(0);     s.insert(prefix[N + 1]);     ded.insert(0);     ded.insert(N + 1);     for (int i = 0; i < N; i++) {       int x;       cin >> x;       ded.insert(x);       int l = 0, r = 0;       auto lt = ded.lower_bound(x);       if (lt == ded.begin()) {         l = 0;       } else {         lt--;         l = *lt;         l++;       }       auto rt = ded.upper_bound(x);       if (rt == ded.end()) {         r = N + 1;       } else {         r = *rt;         r--;       }       s.erase(s.find(range(l, r)));       s.insert(range(l, x - 1));       s.insert(range(x + 1, r));       cout << *(s.rbegin()) <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   string s;   int main() {     cin >> n >> m >> s;     if (m - 1 < n - m) {       while (m > 1) {         cout <<  LEFT n ;         --m;       }       for (int i = 0; i < (int)(n); i++) {         cout <<  PRINT   << s[i] <<   n ;         if (i != n - 1) cout <<  RIGHT n ;       }     } else {       while (m < n) {         cout <<  RIGHT n ;         ++m;       }       for (int i = 0; i < (int)(n); i++) {         cout <<  PRINT   << s[n - 1 - i] <<   n ;         if (i != n - 1) cout <<  LEFT n ;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int cnt = 0;     bool flag = true;     int low = -2000000000, high = 2000000000;     bool lset = false;     bool hset = false;     cin >> cnt;     for (int i = 1; i <= cnt; i++) {       string str1, str2, str3;       cin >> str1 >> str2 >> str3;       stringstream stream(str2);       int num;       stream >> num;       if (((str1 ==  > ) && (str3 ==  Y )) || ((str1 ==  <= ) && (str3 ==  N ))) {         if (low <= num) {           low = num + 1;           lset = true;         }         if (lset && hset && (high < low || low > 2000000000)) {           flag = false;           break;         }       }       if (((str1 ==  >= ) && (str3 ==  Y )) || ((str1 ==  < ) && (str3 ==  N ))) {         if (low < num) {           low = num;           lset = true;         }         if (lset && hset && (high < low || low > 2000000000)) {           flag = false;           break;         }       }       if (((str1 ==  <= ) && (str3 ==  Y )) || ((str1 ==  > ) && (str3 ==  N ))) {         if (high > num) {           high = num;           hset = true;         }         if (lset && hset && (high < low || high < -2000000000)) {           flag = false;           break;         }       }       if (((str1 ==  < ) && (str3 ==  Y )) || ((str1 ==  >= ) && (str3 ==  N ))) {         if (high >= num) {           high = num - 1;           hset = true;         }         if (lset && hset && (high < low || high < -2000000000)) {           flag = false;           break;         }       }     }     if (flag && (!hset) && lset) cout << low << endl;     if (flag && (!lset) && hset) cout << high << endl;     if (flag && !lset && !hset) cout <<  0  << endl;     if (flag && lset && hset) cout << low << endl;     if (!flag) cout <<  Impossible  << endl;   }
#include <bits/stdc++.h>   #pragma GCC optimize( O2 )   using namespace std;   long long n, x, y, nr;   long long cx, cy;   int ans[2000002];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> n >> x >> y;     int xx = 0;     while (xx <= x + y) {       long long pa = (cx + 1) * y;       long long pb = (cy + 1) * x;       if (pa == pb)         ++cx, ++cy, xx += 2;       else if (pa < pb)         ans[xx] = 1, ++cx, ++xx;       else         ans[xx] = 2, ++cy, ++xx;     }     for (int i = 1; i <= n; ++i) {       cin >> nr;       --nr;       nr %= (x + y);       if (ans[nr] == 1) cout <<  Vanya  <<   n ;       if (ans[nr] == 2) cout <<  Vova  <<   n ;       if (ans[nr] == 0) cout <<  Both  <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 5;   const int MAX = 1e9 + 5;   int i, j, w1, w2, k, n, m, a[N], b[N], calca[N], calcb[N], temp;   vector<pair<int, int> > ans, adasd;   bool ctr(int s) {     int set = 0;     int p1 = 0;     int p2 = 0;     int last1 = 0;     int last2 = 0;     int lastt = 0;     while (1) {       if (w1 == last1 && w2 == last2)         if (((p1 > p2 && lastt == 1) || (p2 > p1 && lastt == 2)) && p1 != p2) {           temp = max(p1, p2);           return true;         } else           return false;       if (last2 + s <= w2 && b[last2 + s] < a[last1 + s]) {         p2++;         lastt = 2;         last2 += s;         last1 = lower_bound(a + 1, a + w1 + 1, b[last2]) - a - 1;       } else if (last1 + s <= w1 && a[last1 + s] < b[last2 + s]) {         p1++;         lastt = 1;         last1 += s;         last2 = lower_bound(b + 1, b + w2 + 1, a[last1]) - b - 1;       } else         return false;     }   }   int main() {     cin >> n;     int asdasdasda;     for (int i = 1; i <= 200001; i++) a[i] = b[i] = 100000000;     int x;     for (int i = 1; i <= n; i++) {       scanf( %d , &x);       if (x == 1)         a[++w1] = i;       else         b[++w2] = i;     }     for (int i = 1; i <= n; i++)       if (ctr(i)) ans.push_back(make_pair(temp, i));     sort(ans.begin(), ans.end());     cout << ans.size() <<   n ;     for (int i = 0; i <= (int)ans.size() - 1; i++)       cout << ans[i].first <<     << ans[i].second <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, i, j, k, l, maxx, minn, c = 1, c2 = 0, cnt = 0, a1 = 0, a2 = 0;     cin >> n;     long long a[n], b[n];     cnt = n;     for (i = 0; i < n; i++) {       cin >> a[i];       b[i] = a[i];     }     sort(b, b + n);     maxx = b[n - 1];     minn = b[0];     for (i = 0; i < n; i++) {       if (a[i] == a[i + 1])         ++c;       else {         a1 = a1 + ((c - 1) * c) / 2;         c = 1;       }     }     a1 = a1 + ((c - 1) * c) / 2;     cnt = cnt + a1;     cout << cnt << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   int q[N];   int n;   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     cin >> n;     for (int i = 1; i <= n; i++) cin >> q[i];     if (n & 1) {       cout <<  YES  <<   n ;       cout << n - 1 <<   n ;       for (int i = 1; i < n; i += 2) {         cout << i <<     << i + 1 <<     << i + 2 <<   n ;       }       for (int i = 1; i < n; i += 2) {         cout << i <<     << i + 1 <<     << n <<   n ;       }     } else {       int tmp = 0;       for (int i = 1; i <= n; i++) tmp ^= q[i];       if (tmp != 0) {         cout <<  NO  <<   n ;       } else {         cout <<  YES  <<   n ;         cout << n - 2 <<   n ;         for (int i = 1; i < n - 1; i += 2) {           cout << i <<     << i + 1 <<     << i + 2 <<   n ;         }         for (int i = 1; i < n - 1; i += 2) {           cout << i <<     << i + 1 <<     << n - 1 <<   n ;         }       }     }   }
#include <bits/stdc++.h>   using namespace std;   bool mayorigual(const string &s, int i, int ifin, int j, int jfin) {     if (ifin - i > jfin - j) return true;     if (ifin - i < jfin - j) return false;     int len = ifin - i;     for (int k = 0; k < len; k++) {       if (s[k + i] > s[k + j])         return true;       else if (s[k + i] < s[k + j])         return false;     }     return true;   }   int main() {     int ret = 0;     string s;     cin >> s;     int j = s.size();     int i = j - 1;     while (i >= 0 && j > 0) {       if (s[i] ==  0 )         i--;       else if (mayorigual(s, 0, i, i, j)) {         ret++;         j = i;         i = j - 1;       } else {         ret++;         break;       }     }     cout << ret << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 131072, P = 998244353, inv2 = (P + 1) / 2;   int n, m, ans, a[maxn], b[maxn], fact[maxn], finv[maxn];   int lim, l, r[maxn], w[maxn];   vector<int> V;   vector<int> f[maxn << 2][2][2];   int qp(int x, int y) {     int z = 1;     for (; y; y >>= 1, x = 1LL * x * x % P) {       if (y & 1) z = 1LL * z * x % P;     }     return z;   }   void init() {     for (int i = 0; i < lim; i++) r[i] = (r[i >> 1] >> 1) | ((i & 1) << (l - 1));     w[lim >> 1] = 1;     int t = qp(3, (P - 1) / lim);     for (int i = (lim >> 1) + 1; i < lim; i++) w[i] = 1LL * t * w[i - 1] % P;     for (int i = (lim >> 1) - 1; i > 0; i--) w[i] = w[i << 1];   }   void DFT(int *a, int type) {     static unsigned long long c[maxn];     copy(a, a + lim, c);     for (int i = 0; i < lim; i++)       if (i < r[i]) swap(c[i], c[r[i]]);     for (int i = 1; i < lim; i <<= 1) {       for (int j = 0; j < lim; j += i << 1) {         for (int k = 0; k < i; k++) {           unsigned long long t = c[i + j + k] * w[i + k] % P;           c[i + j + k] = c[j + k] + P - t, c[j + k] += t;         }       }     }     if (!~type) {       reverse(c + 1, c + lim);       for (int i = 0, t = qp(lim, P - 2); i < lim; i++) c[i] = c[i] % P * t % P;     }     for (int i = 0; i < lim; i++) a[i] = c[i] % P;   }   vector<int> operator*(vector<int> A, vector<int> B) {     int len = A.size() + B.size() - 1;     for (lim = 1, l = 0; lim < len; lim <<= 1) l++;     A.resize(lim), B.resize(lim);     init(), DFT(A.data(), 1), DFT(B.data(), 1);     for (int i = 0; i < lim; i++) A[i] = 1LL * A[i] * B[i] % P;     DFT(A.data(), -1), A.resize(len);     return A;   }   int main() {     scanf( %d , &n);     for (int i = fact[0] = finv[0] = 1; i <= n; i++) {       scanf( %d , &a[i]);       fact[i] = 1LL * i * fact[i - 1] % P;       finv[i] = qp(fact[i], P - 2);     }     for (int i = 1; i <= n; i += a[i]) {       b[++m] = a[i];       for (int j = i; j < i + a[i]; j++) {         if (a[i] ^ a[j]) puts( 0 ), exit(0);       }     }     function<void(int, int, int)> solve = [&](int k, int l, int r) {       if (l == r) {         f[k][0][0] = {0, b[l] == 1 ? inv2 : 1};         f[k][0][1] = f[k][1][0] = f[k][1][1] = {0, 1};         return;       }       int mid = (l + r) / 2, ls = k << 1, rs = k << 1 | 1;       solve(ls, l, mid), solve(rs, mid + 1, r);       for (int p : {0, 1})         for (int q : {0, 1}) {           f[k][p][q].resize(r - l + 2);         }       for (int a : {0, 1})         for (int b : {0, 1})           for (int c : {0, 1}) {             vector<int> g = f[ls][a][b] * f[rs][b][c];             for (int i = b; i < g.size(); i++) (f[k][a][c][i - b] += g[i]) %= P;           }     };     solve(1, 1, m);     for (int i = 1; i <= m; i++) {       ans = (ans + 1LL * fact[i] * f[1][0][0][i] % P * qp(2, i) % P *                        ((m - i) & 1 ? P - 1 : 1)) %             P;     }     printf( %d n , ans);     return 0;   }