func_code_string: #include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 7;   int n, lim, a[N], mn[N], mx[N], suf[N];   long long ans;   int main() {     scanf( %d%d , &n, &lim);     for (int i = 1; i <= lim; i++) mn[i] = n + 1;     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       mn[a[i]] = min(mn[a[i]], i);       mx[a[i]] = max(mx[a[i]], i);     }     int L = lim, R = 1, pre = mx[1];     for (int i = 1; i < lim; i++) {       pre = max(pre, mx[i]);       if (pre > mn[i + 1]) {         L = i;         break;       }     }     if (L == lim) {       cout << 1ll * lim * (lim + 1) / 2;       return 0;     }     pre = mn[lim];     for (int i = lim; i > 1; i--) {       pre = min(pre, mn[i]);       if (mx[i - 1] > pre) {         R = i;         break;       }     }     ans = lim - R + 2;     suf[lim] = mn[lim];     for (int i = lim - 1; i >= R; i--) suf[i] = min(suf[i + 1], mn[i]);     pre = mx[1];     for (int i = 1, j = R; i <= L; i++) {       pre = max(pre, mx[i]);       while (j <= lim && pre > suf[j]) j++;       ans += lim - j + 2;       if (j == i + 1) ans--;     }     cout << ans;   }   