func_code_string: #include <bits/stdc++.h>   int n, m;   std::vector<std::pair<int, int> > a[2];   void normalize(std::vector<std::pair<int, int> >& v) {     if (v.empty()) {       return;     }     std::sort((v).begin(), (v).end());     int k = 1;     for (int i = 1; i < ((int)(v).size()); i++) {       if (v[i].first != v[i - 1].first) {         v[k++] = v[i];       } else {         v[k - 1].second += v[i].second;       }     }     v.erase(v.begin() + k, v.end());   }   int calc(std::vector<std::pair<int, int> >& a,            std::vector<std::pair<int, int> >& b) {     if (((int)(a).size()) == 0) {       return 0;     }     int max = a.back().first - a.front().first;     for (int i = 0; i < ((int)(b).size()); i++) {       if (b[i].first <= a.front().first) {         max = std::max(max, n - (a.front().first - b[i].first));       }       if (b[i].first >= a.back().first) {         max = std::max(max, n - (b[i].first - a.back().first));       }     }     std::vector<std::pair<int, int> >::iterator itr =         std::lower_bound((b).begin(), (b).end(), std::make_pair(a[0].first, 0));     if (itr != b.end() && itr->first <= a.back().first) {       max = n;     }     return max;   }   long long binom(int n) { return 1LL * n * (n - 1) * (n - 2) / 6; }   long long COUNT(std::vector<std::pair<int, int> >& a,                   std::vector<std::pair<int, int> >& b, int max) {     if (calc(a, b) < max) {       return 0LL;     }     long long ways = 0;     int sum = 0, cnt = 0;     for (int i = 0; i < ((int)(a).size()); i++) {       sum += a[i].second;     }     for (int i = 0; i < ((int)(b).size()); i++) {       cnt += b[i].second;     }     std::vector<int> suffix(((int)(a).size()) + 1, 0);     for (int i = ((int)(a).size()) - 1; i >= 0; i--) {       suffix[i] = suffix[i + 1] + a[i].second;     }     if (a.back().first - a.front().first == max) {       ways += binom(sum);       ways -= binom(sum - a.front().second) + binom(sum - a.back().second);       ways += binom(sum - a.front().second - a.back().second);       ways += 1LL * a.front().second * a.back().second * cnt;     }     if (((int)(b).size()) > 0) {       for (int i = 0; i < ((int)(b).size()); i++) {         if (b[i].first < a.front().first) {           int tmp = n - (a.front().first - b[i].first);           if (tmp == max) {             ways +=                 1LL * a.front().second * (a.front().second - 1) / 2 * b[i].second;             ways +=                 1LL * a.front().second * (sum - a.front().second) * b[i].second;           }         }         if (b[i].first > a.back().first) {           int tmp = n - (b[i].first - a.back().first);           if (tmp == max) {             ways +=                 1LL * a.back().second * (a.back().second - 1) / 2 * b[i].second;             ways += 1LL * a.back().second * (sum - a.back().second) * b[i].second;           }         }       }       if (max == n) {         for (int i = 0; i < ((int)(b).size()); i++) {           std::vector<std::pair<int, int> >::iterator itr = std::lower_bound(               (a).begin(), (a).end(), std::make_pair(b[i].first, 0));           int p = 0, q = 0, r = 0;           p = suffix[0] - suffix[itr - a.begin()];           int t = itr - a.begin();           itr = std::lower_bound((a).begin(), (a).end(),                                  std::make_pair(b[i].first + 1, 0));           q = suffix[t] - suffix[itr - a.begin()];           r = suffix[0] - p - q;           ways += 1LL * q * p * b[i].second;           ways += 1LL * q * r * b[i].second;           ways += 1LL * p * r * b[i].second;           ways += 1LL * q * (q - 1) / 2 * b[i].second;         }       }     }     return ways;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < m; i++) {       int s, f;       scanf( %d%d , &s, &f);       a[s].push_back(std::make_pair(f, 1));     }     normalize(a[0]);     normalize(a[1]);     int max = std::max(calc(a[0], a[1]), calc(a[1], a[0]));     if (max == 0) {       std::cout << binom(m) << std::endl;     } else {       std::cout << COUNT(a[0], a[1], max) + COUNT(a[1], a[0], max) << std::endl;     }   }   