malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int maxm = 1.5e7;   const int maxn = 1e5 + 5;   int lson[maxm], rson[maxm];   long long sum[maxm];   int T[maxn];   int clk, n;   void modity(int &rt, int L, int R, int pos, int val) {     if (!rt) rt = ++clk;     if (L == R) {       sum[rt] += val;     } else {       int mid = L + R >> 1;       if (pos <= mid)         modity(lson[rt], L, mid, pos, val);       else         modity(rson[rt], mid + 1, R, pos, val);       sum[rt] = sum[lson[rt]] + sum[rson[rt]];     }   }   long long query(int rt, int L, int R, int l, int r) {     if (!rt) return 0;     if (l <= L && R <= r)       return sum[rt];     else {       int mid = L + R >> 1;       long long ret = 0;       if (l <= mid) ret += query(lson[rt], L, mid, l, r);       if (mid < r) ret += query(rson[rt], mid + 1, R, l, r);       return ret;     }   }   int lowbit(int x) { return x & -x; }   void modity(int pos, pair<int, int> val) {     for (int i = pos; i <= n; i += lowbit(i)) {       modity(T[i], 1, n, val.first, val.second);     }   }   long long query(int l, int r) {     long long ret = 0;     for (int i = r; i > 0; i -= lowbit(i)) ret += query(T[i], 1, n, l, n);     for (int i = l - 1; i > 0; i -= lowbit(i)) ret -= query(T[i], 1, n, l, n);     return ret;   }   set<int> s[maxn];   int a[maxn];   int main() {     int m;     scanf( %d %d , &n, &m);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       if (s[a[i]].size()) {         int prev = *s[a[i]].rbegin();         modity(i, make_pair(prev, i - prev));       }       s[a[i]].insert(i);     }     while (m--) {       int opt;       scanf( %d , &opt);       if (opt == 1) {         int pos, val;         scanf( %d %d , &pos, &val);         if (val == a[pos]) continue;         auto it = s[a[pos]].find(pos);         auto is = it;         it++;         if (it != s[a[pos]].end()) {           modity(*it, make_pair(*is, *is - *it));           if (is != s[a[pos]].begin()) {             auto ix = is;             ix--;             modity(*it, make_pair(*ix, *it - *ix));           }         }         if (is != s[a[pos]].begin()) {           auto ix = is;           ix--;           modity(*is, make_pair(*ix, *ix - *is));         }         s[a[pos]].erase(pos);         a[pos] = val;         is = s[a[pos]].insert(pos).first;         if (is != s[a[pos]].begin()) {           auto ix = is;           ix--;           modity(*is, make_pair(*ix, *is - *ix));         }         it = is;         it++;         if (it != s[a[pos]].end()) {           if (is != s[a[pos]].begin()) {             auto ix = is;             ix--;             modity(*it, make_pair(*ix, *ix - *it));           }           modity(*it, make_pair(*is, *it - *is));         }       } else {         int l, r;         scanf( %d %d , &l, &r);         long long ret = query(l, r);         printf( %lld n , ret);       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007;   const long long maxn = 1e5 + 10;   vector<long long> a[maxn];   long long lo[maxn], hi[maxn];   const long long inf = 1e15;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout << fixed << setprecision(9);     long long n;     cin >> n;     long long cnt = 0;     map<long long, long long> mp;     vector<long long> keep;     for (long long i = 0; i < n; ++i) {       long long len;       cin >> len;       bool ok = false;       long long mn = inf;       long long mx = -inf;       for (long long j = 0; j < len; ++j) {         long long cur;         cin >> cur;         a[i].push_back(cur);         if (cur > mn) ok = true;         mn = min(mn, cur);         mx = max(mx, cur);       }       lo[i] = mn;       hi[i] = mx;       if (ok) {         ++cnt;         hi[i] = inf;       } else {         keep.push_back(mx);       }     }     sort(keep.begin(), keep.end());     long long res = 0;     for (long long i = 0; i < n; ++i) {       if (hi[i] == inf) {         res += n;       } else {         long long cur = cnt;         auto it = upper_bound(keep.begin(), keep.end(), lo[i]);         if (it != keep.end()) {           long long ind = it - keep.begin();           long long is = (long long)keep.size() - ind;           cur += is;         }         res += cur;       }     }     cout << res <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 100;   int a[maxn], ans[maxn];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n, m, c;     cin >> n >> m >> c;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < m; i++) {       int val;       cin >> val;       ans[i] += val;       ans[i + n - m + 1] -= val;     }     ans[0] %= c;     for (int i = 1; i < n; i++) ans[i] = (ans[i] + ans[i - 1]) % c;     for (int i = 0; i < n; i++) cout << ((a[i] + ans[i]) % c + c) % c <<    ;     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int i, j, k, l, n, m, s, tot, ss, t;   const int pp = 1000000007;   long long a[100010], an;   void dfs(int x, bitset<120> y, long long s) {     if (x > m / 2) {       an = (an + s) % pp;       return;     }     dfs(x + 1, y, s);     if ((a[x]) && (!y[x]))       dfs(x + 1, y | (y << x) | (y >> x) | (y << (m - x)) | (y >> (m - x)),           (s * a[x]) % pp);   }   int main() {     scanf( %d%d%d , &n, &m, &t);     for (int i = 1; i <= t; i++) {       int x;       scanf( %d , &x);       x %= m;       a[min(x, m - x)]++;     }     dfs(0, bitset<120>(1), 1);     printf( %I64d , an);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int t;     cin >> t;     while (t--) {       long long int n, k;       cin >> n >> k;       vector<set<long long int>> v(k);       for (long long int i = 0; i < n; ++i) {         char c;         cin >> c;         if (c ==  ? ) continue;         if (c ==  1 )           v[i % k].insert(1);         else           v[i % k].insert(2);       }       bool ans = true;       long long int count_0 = 0, count_1 = 0, c_q = 0;       for (auto s : v) {         if (s.size() > 1) ans = false;       }       for (auto s : v) {         if (s.empty()) {           c_q++;           continue;         }         if (*s.begin() == 1) {           count_1++;           continue;         } else {           count_0++;           continue;         }       }       if (c_q < abs(count_0 - count_1)) {         ans = false;       }       if (!ans) {         cout <<  NO               <<   n ;       } else {         cout <<  YES               <<   n ;       }     }   }
#include <bits/stdc++.h>   using namespace std;   #define ll long long            int main()   {          ios_base::sync_with_stdio(false);       cin.tie(NULL);       int t;       cin >> t;       int n, k;       while (t--)       {           char a[3] = { a , b , c };           cin >> n >> k;            int temp = k;            while(k--) {               cout<<a[0];           }           int i = 1;           n = n - temp;           //cout << n;           while(n--) {                              if(i == 3){                   i = 0;               }               cout<<a[i];               i++;           }              cout << endl;       }          return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool check(long long mid, vector<long long> &d, vector<long long> &prep) {     long long n = d.size();     long long m = prep.size() - 1;     vector<long long> pos(m + 1, -1);     for (long long i = 0; i <= mid; i++) pos[d[i]] = i;     for (long long i = 1; i <= m; i++)       if (pos[i] == -1) return false;     long long prep_days = 0;     for (long long i = 0; i <= mid; i++) {       if (d[i] == 0)         prep_days++;       else if (pos[d[i]] == i) {         if (prep_days < prep[d[i]])           return false;         else           prep_days -= prep[d[i]];       } else         prep_days++;     }     return true;   }   int32_t main() {     long long n, m;     cin >> n >> m;     vector<long long> d(n);     for (long long &i : d) cin >> i;     vector<long long> prep(m + 1);     for (long long i = 1; i <= m; i++) cin >> prep[i];     long long l = 0, r = n - 1;     while (l < r) {       long long mid = (l + r) / 2;       if (check(mid, d, prep)) {         r = mid;       } else {         l = mid + 1;       }     }     if (r == n - 1) {       if (check(r, d, prep))         cout << n <<   n ;       else         cout << -1 <<   n ;     } else       cout << r + 1 <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename T>   int sz(const T &a) {     return (int)a.size();   }   long long int am[101];   int main() {     int n;     cin >> n;     long long int trip;     long long int str;     cin >> trip >> str;     long long int a, b;     bool lastinc = false;     cout <<  +   << 1 << endl;     cin >> trip >> str;     cout <<  +   << 2 << endl;     cin >> a >> b;     if (b > str)       lastinc = true;     else       lastinc = false;     trip = a, str = b;     cout <<  +   << 3 << endl;     cin >> a >> b;     if (a > trip) {       am[3] = (long long int)sqrt(2 * (a - trip)) + 2;     } else {       if (lastinc)         am[3] = 2;       else         am[3] = 1;     }     long long int diff = b - str;     trip = a, str = b;     cout <<  +   << 1 << endl;     cin >> a >> b;     am[1] = (long long int)sqrt(2 * (a - trip)) + 2;     am[2] = (b - str) / am[3];     if (diff > (am[1] - 1) * (am[2]))       lastinc = true;     else       lastinc = false;     trip = a, str = b;     for (int i = 4; i < n; i++) {       cout <<  +   << i << endl;       cin >> a >> b;       if (a > trip) {         am[i] = (long long int)sqrt(2 * (a - trip)) + 1;       } else {         if (lastinc)           am[i] = 1;         else           am[i] = 0;       }       am[i]++;       if (b - str > (am[i - 2]) * (am[i - 1]))         lastinc = true;       else         lastinc = false;       trip = a, str = b;     }     for (int i = 3; i < n; i++) {       str -= am[i] * am[i - 1] * am[i - 2];     }     am[n] = (str / am[n - 1] / am[n - 2]) + 1;     am[1]--;     cout <<  !  ;     for (int i = 1; i <= n; i++) cout << am[i] - 1 <<    ;     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int Ss[10005], Ee[10005];   int pre[10005 * 200][26];   char A[10005], s[10005], e[10005];   int main() {     scanf( %s%s%s , A, s, e);     int n = strlen(A);     int lens = strlen(s);     int lene = strlen(e);     int i, j;     memset(Ss, 0, sizeof(Ss));     memset(Ee, 0, sizeof(Ee));     memset(pre, -1, sizeof(pre));     for (i = 0; i <= n - lens; i++) {       for (j = 0; j < lens; j++)         if (A[i + j] != s[j]) break;       if (j >= lens) Ss[i] = 1;     }     for (i = 0; i <= n - lene; i++) {       for (j = 0; j < lene; j++)         if (A[i + j] != e[j]) break;       if (j >= lene) Ee[i + lene - 1] = 1;     }     int m = (lens > lene ? lens : lene);     int cnt = 0;     int alloc = 1;     for (i = 0; i <= n - m; i++) {       if (!Ss[i]) continue;       int cur = 0;       for (j = i; j < i + m - 1; j++) {         if (pre[cur][A[j] -  A ] == -1) pre[cur][A[j] -  A ] = alloc++;         cur = pre[cur][A[j] -  A ];       }       for (j = i + m - 1; j < n; j++) {         if (Ee[j])           if (pre[cur][A[j] -  A ] == -1) cnt++;         if (pre[cur][A[j] -  A ] == -1) pre[cur][A[j] -  A ] = alloc++;         cur = pre[cur][A[j] -  A ];       }     }     printf( %d n , cnt);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long N = 1e3 + 7;   long long a[N][N];   int32_t main() {     long long n;     cin >> n;     for (long long i = 0; i < n; i++)       for (long long j = 0; j < n; j++) cin >> a[i][j];     long long a12 = a[0][1], a23 = a[1][2], a13 = a[0][2];     long double aa123 = (sqrt(a12 * 1.0) * sqrt(a23 * 1.0) * sqrt(a13 * 1.0));     long long a123 = round(aa123);     long long a1 = a123 / a23;     cout << a1 <<    ;     for (long long i = 1; i < n; i++) cout << a[0][i] / a1 <<    ;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 10;   const int mod = 1e9 + 7;   int n;   struct block {     vector<int> v;     int id;   };   vector<block> b;   int a[maxn];   void get_blocks() {     b.clear();     block cur;     int j = 1;     while (j <= n) {       cur.v.clear();       while (1) {         cur.v.push_back(a[j]);         if (j == n || a[j + 1] != a[j] + 1) {           ++j;           break;         }         ++j;       }       b.push_back(cur);     }     vector<int> bv;     for (auto& w : b) bv.push_back(w.v[0]);     sort(bv.begin(), bv.end());     for (auto& w : b) {       int u = lower_bound(bv.begin(), bv.end(), w.v[0]) - bv.begin() + 1;       w.id = u;     }   }   vector<vector<int>> tv;   void tr() {     vector<vector<block>> seq;     int la = b.back().id;     int en = b.size() - 1;     vector<block> last;     last.push_back(b.back());     vector<int> e;     vector<block> mid;     if (la == b.size()) {       la = b[b.size() - 2].id;       vector<block> q;       q.push_back(b[b.size() - 2]);       --en;       for (int i = 0; i < b.size(); ++i) {         if (b[i].id == la + 1) {           for (int j = i; j < en; ++j) mid.push_back(b[j]);           vector<block> fr;           for (int j = 0; j < i; ++j) fr.push_back(b[j]);           if (fr.size()) seq.push_back(fr);           seq.push_back(mid);           seq.push_back(q);           seq.push_back(last);           break;         }       }       goto N;     }     for (int i = 0; i < b.size(); ++i) {       if (b[i].id == la + 1) {         for (int j = i; j < en; ++j) mid.push_back(b[j]);         vector<block> fr;         for (int j = 0; j < i; ++j) fr.push_back(b[j]);         if (fr.size()) seq.push_back(fr);         seq.push_back(mid);         seq.push_back(last);         break;       }     }   N:;     e.push_back(seq.size());     for (auto& w : seq) {       int cnt = 0;       for (auto& bb : w) cnt += bb.v.size();       e.push_back(cnt);     }     tv.push_back(e);     reverse(seq.begin(), seq.end());     int j = 1;     for (auto& w : seq) {       for (auto& ee : w) {         for (auto& ii : ee.v) a[j++] = ii;       }     }   }   int main() {     cin >> n;     for (int i = 1; i <= n; ++i) cin >> a[i];     while (1) {       get_blocks();       if (b.size() == 1) break;       tr();     }     cout << tv.size() <<   n ;     for (auto& w : tv) {       for (int i = 0; i < w.size(); ++i) {         cout << w[i];         if (i < w.size() - 1)           cout <<    ;         else           cout <<   n ;       }     }   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       vector<int> dp(2 * n);       int mx = -1;       int c = 0;       dp[0] = 1;       for (int i = 0; i < 2 * n; i++) {         int u;         cin >> u;         if (mx > u) {           c++;         } else {           for (int j = n + 1; j >= c; j--) {             dp[j] |= dp[j - c];           }           c = 1;           mx = u;         }       }       for (int j = n; j >= c; j--) {         dp[j] |= dp[j - c];       }       if (dp[n])         cout <<  YES  << endl;       else         cout <<  NO  << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int A[101];     int B[101];     int n, k;     cin >> n >> k;     for (int i = 0; i < n; i++) cin >> A[i];     int t = n / k;     for (int i = 0; i < k; i++) {       int max1 = 0;       int max2 = 0;       for (int j = 0; j < t; j++)         if (A[i + j * k] == 1)           max1++;         else           max2++;       if (max2 > max1)         B[i] = 2;       else         B[i] = 1;     }     int d = 0;     for (int i = 0; i < t; i++) {       for (int j = 0; j < k; j++)         if (A[i * k + j] != B[j]) d++;     }     cout << d;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void A() {     cout <<  sjfnb  << endl;     exit(0);   }   void B() {     cout <<  cslnb  << endl;     exit(0);   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     vector<int> a(n);     for (int i = 0; i < n; ++i) {       cin >> a[i];     }     sort(a.begin(), a.end());     if (n == 1) {       if (a[0] % 2 == 0) {         B();       } else {         A();       }     }     if (a[1] == 0) {       B();     }     vector<int> b = a;     if (unique(b.begin(), b.end()) - b.begin() <= n - 2) {       B();     }     int t = 0;     for (int i = 0; i < n - 1; ++i) {       if (a[i] == a[i + 1]) {         if (i > 0 && a[i - 1] == a[i] - 1) {           B();         }         --a[i];         t = 1;       }     }     for (int i = 0; i < n; ++i) {       t = (t + a[i] - i) % 2;     }     if (t % 2 == 0) {       B();     } else {       A();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long bins(long long l, long long r) {     while (l + 1 < r) {       long long m = (l + r) >> 1;       cout <<  ?   << l <<     << m << endl;       cout.flush();       char c;       cin >> c;       if (c ==  e ) return 0;       if (c ==  y ) {         l = m;       } else {         r = m;       }     }     return r;   }   signed main() {     string s;     cin >> s;     long long x, y;     while (s ==  start ) {       x = 0, y = 1;       char c =  y ;       bool z = false;       while (c !=  x ) {         if (!z)           z = true;         else           x = y, y *= 2;         cout <<  ?   << x <<     << y << endl;         cout.flush();         cin >> c;         if (c ==  e ) return 0;       }       long long p = bins(x, y);       cout <<  !   << p << endl;       cout.flush();       cin >> s;       if (s ==  mistake ) {         return 0;       }       if (s ==  end ) return 0;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const double pi = 2 * acos(0.0);   const int inf = 0x3f3f3f3f;   const double infd = 1.0 / 0.0;   long long power(long long x, long long y, long long MOD) {     long long res = 1;     x = x % MOD;     while (y > 0) {       if (y & 1) res = (res * x) % MOD;       y = y >> 1;       x = (x * x) % MOD;     }     return res;   }   long long mul(long long a, long long b, long long MOD) {     if (b == 1) {       return a;     }     if (b % 2 == 0) {       return 2 * (mul(a, b / 2, MOD) % MOD);     } else {       return (a + (2 * (mul(a, b / 2, MOD)) % MOD)) % MOD;     }   }   bool ispow2(long long n) {     if (((n & (n - 1)) == 0) && n != 0) {       return true;     }     return false;   }   bool prime(int x) {     if (x == 1) {       return false;     }     if (x == 2) {       return true;     }     if (x % 2 == 0) {       return false;     }     for (int i = 3; i * i <= x; i += 2) {       if (x % i == 0) {         return false;       }     }     return true;   }   int __gcd(int a, int b) {     if (b == 0) return a;     return __gcd(b, a % b);   }   int coprime(int a, int b) {     if (a == b) return 0;     if (prime(a) && prime(b)) return 1;     if ((a == 2 && b % 2 != 0) || (b == 2 && a % 2 != 0)) return 1;     if ((a % 2 != 0 && prime(b) && a < b) || (b % 2 != 0 && prime(a) && a > b))       return 1;     if (abs(a - b) == 1) return 1;     if (a == 1 || b == 1) return 1;     return __gcd(a, b);   }   unsigned long long lcm(unsigned a, unsigned b) {     return ((unsigned long long)a) * (b / __gcd(a, b));   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n, diff;     string s;     cin >> n;     cin >> s;     map<int, int> m;     int ans = 0;     m[0] = -1;     diff = 0;     for (int i = 0; i < n; i++) {       int tmp = (s[i] ==  0  ? -1 : 1);       diff += tmp;       if (m.find(diff) == m.end()) {         m[diff] = i;       }       ans = max(ans, i - m[diff]);     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MX = 25;   int main(int argc, char *argv[]) {     ios ::sync_with_stdio(false);     cin.tie(NULL);     int n, k;     string s;     cin >> n >> k;     cin >> s;     if (k > (n + 1) / 2) {       --k;       while (k < n - 1) {         cout <<  RIGHT  <<   n ;         ++k;       }       while (k > -1) {         cout <<  PRINT   << s[k] <<   n ;         if (k - 1 >= 0) cout <<  LEFT  <<   n ;         --k;       }     } else {       --k;       while (k > 0) {         cout <<  LEFT  <<   n ;         --k;       }       while (k < n) {         cout <<  PRINT   << s[k] <<   n ;         if (k + 1 < n) cout <<  RIGHT  <<   n ;         ++k;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, a[212345], ans = (1ll << 62);   int main() {     ios_base::sync_with_stdio(false);     cin >> n;     for (long long i = 0; i < 2 * n; ++i) {       cin >> a[i];     }     sort(a, a + 2 * n);     for (long long i = n; i < 2 * n - 1; ++i) {       ans = min(ans, (a[2 * n - 1] - a[0]) * (a[i] - a[i - n + 1]));     }     ans = min(ans, (a[n - 1] - a[0]) * (a[2 * n - 1] - a[n]));     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2005, mod = 998244353;   int n, a[N][N], f[N][N], jie[N], ans, vis1[N], vis2[N], c[N], c2[N], vis3[N];   inline int qpow(int a, int b, int res = 1) {     for (; b; b >>= 1, a = 1ll * a * a % mod)       if (b & 1) res = 1ll * res * a % mod;     return res;   }   void init() {     jie[0] = 1;     for (register int i = 1; i <= n; ++i) jie[i] = 1ll * jie[i - 1] * i % mod;     f[0][0] = 1, f[1][1] = 1;     for (register int i = 2; i <= n; i++) {       f[i][0] = 1ll * (i - 1) * (f[i - 1][0] + f[i - 2][0]) % mod;       for (register int j = 1; j <= i; j++)         f[i][j] = (f[i][j - 1] + f[i - 1][j - 1]) % mod;     }   }   inline int lowbit(int x) { return x & -x; }   inline void add(int x, int y) {     for (; x <= n; x += lowbit(x)) c[x] += y;   }   inline int get(int x, int res = 0) {     for (; x; x -= lowbit(x)) res += c[x];     return res;   }   inline void add2(int x, int y) {     for (; x <= n; x += lowbit(x)) c2[x] += y;   }   inline int get2(int x, int res = 0) {     for (; x; x -= lowbit(x)) res += c2[x];     return res;   }   signed main() {     scanf( %d , &n);     init();     for (register int i = 1; i <= n; ++i) {       int tmp = qpow(f[n][0], n - i), res = 0, num = 0;       memset(vis1, 0, sizeof(vis1)), memset(vis2, 0, sizeof(vis2));       memset(vis3, 0, sizeof(vis3));       memset(c, 0, sizeof(c)), memset(c2, 0, sizeof(c2));       for (register int j = 1; j <= n; ++j) add(j, 1), add2(j, 1);       for (register int j = 1; j <= n; ++j) {         scanf( %d , &a[i][j]);         if (i == 1) {           int cnt = get(a[i][j] - 1);           res = (res + 1ll * cnt * jie[n - j] % mod) % mod;           add(a[i][j], -1);         } else {           vis1[a[i - 1][j]] = 1;           if (vis3[a[i - 1][j]] == 0)             add2(a[i - 1][j], -1), vis3[a[i - 1][j]] = 1;           if (vis2[a[i - 1][j]]) num--;           int cnt = get(a[i][j] - 1);           if (a[i - 1][j] < a[i][j] && get(a[i - 1][j]) - get(a[i - 1][j] - 1))             cnt--;           int cnt1 = get2(a[i][j] - 1), cnt2 = 0;           if (a[i - 1][j] < a[i][j] && get2(a[i - 1][j]) - get2(a[i - 1][j] - 1))             cnt1--;           cnt2 = cnt - cnt1;           res = (res + 1ll * cnt1 * f[n - j][num + 1] % mod) % mod;           res = (res + 1ll * cnt2 * f[n - j][num] % mod) % mod;           vis2[a[i][j]] = 1;           add(a[i][j], -1);           if (!vis3[a[i][j]]) add2(a[i][j], -1), vis3[a[i][j]] = 1;           if (!vis1[a[i][j]]) num++;         }       }       ans = (ans + 1ll * res * tmp % mod) % mod;     }     cout << ans <<   n ;     return 0;   }