#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define repi(i, a, b) for(int i=(a); i>(b); i--)
#define db(x) (cerr << #x << ": " << (x) << '\n')
#define sync ios_base::sync_with_stdio(false), cin.tie(NULL)
#define iceil(n, x) (((n) + (x) - 1) / (x))
#define ll long long
#define mp make_pair
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define sz size()
#define all(v) (v).begin(), (v).end()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll, ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define vvi vector<vi>
#define fi first
#define se second
#define umap unordered_map
#define uset unordered_set
#define pqueue priority_queue
#define si(a) scanf("%d", &a)
#define sll(a) scanf("%lld", &a)
#define bitcount(x) __builtin_popcount(x)
#define cps CLOCKS_PER_SEC
#define PI acos(-1.0)
#define EPS 1e-9
#define mod 1000000007
#define MOD 1000000007
#define N 100005
#define M 50
using namespace std;
template<typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;
template<typename T>
using maxpq = priority_queue<T>;
//All indexing is 0-based
using namespace __gnu_pbds;
typedef tree<pii, null_type, less<pii>, rb_tree_tag,
tree_order_statistics_node_update> ordered_set;
//methods: find_by_order(k); & order_of_key(k);
//To make it an ordered_multiset, use pairs of (value, time_of_insertion)
//to distinguish values which are similar.
//a and b are assumed to be taken modulo p
inline int add(int a, int b, int p = mod){ ll c = a + b; if(c >= p) c -= p; return c;}
inline int sub(int a, int b, int p = mod){ int c = a - b; if(c < 0) c += p; return c;}
inline int mul(int a, int b, int p = mod){ return (a * 1ll * b) % p;}
int a[N];
class MajoritySubarray {
public:
long long getCount(int n, int seed, int m) {
//::n = n;
rep(i, 0, n) {
a[i] = (seed >> 16) % m;
//has[a[i]] = 1;
seed = (seed * 1103515245ll + 12345) & ((1ll << 31) - 1);
}
//int bal = n;
ll ans = 0;
rep(k, 0, m) {
ordered_set os;
int bal = 0;
int t = 0;
rep(i, 0, n) {
os.insert({bal, ++t});
if(a[i] == k) bal++;
else bal--;
ans += os.order_of_key({bal, 0});
//db(bal), db(k), db(i), db(ans);
}
}
return ans;
}
};
main()
{
#ifndef ONLINE_JUDGE
freopen("/home/tarun/Desktop/input.txt", "r", stdin);
// freopen("/home/tarun/Desktop/output.txt", "w", stdout);
#endif
sync;
clock_t clk = clock();
//cout << MajoritySubarray().getCount(100000, 2147483647, 2) << '\n';
cout << MajoritySubarray().getCount(100000, 54534555, 50) << '\n';
cerr << "Time (in ms): " << double(clock() - clk) * 1000.0 / cps << '\n';
}
//Compile using:
//g++ -o filename.exe --std=c++11 filename.cpp
//Use -D CP for defining a macro CP in the local environment
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2luY2x1ZGU8ZXh0L3BiX2RzL2Fzc29jX2NvbnRhaW5lci5ocHA+CiNpbmNsdWRlPGV4dC9wYl9kcy90cmVlX3BvbGljeS5ocHA+CiNkZWZpbmUgcmVwKGksIGEsIGIpIGZvcihpbnQgaT0oYSk7IGk8KGIpOyBpKyspCiNkZWZpbmUgcmVwaShpLCBhLCBiKSBmb3IoaW50IGk9KGEpOyBpPihiKTsgaS0tKQojZGVmaW5lIGRiKHgpIChjZXJyIDw8ICN4IDw8ICI6ICIgPDwgKHgpIDw8ICdcbicpCiNkZWZpbmUgc3luYyBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKGZhbHNlKSwgY2luLnRpZShOVUxMKQojZGVmaW5lIGljZWlsKG4sIHgpICgoKG4pICsgKHgpIC0gMSkgLyAoeCkpCiNkZWZpbmUgbGwgbG9uZyBsb25nCiNkZWZpbmUgbXAgbWFrZV9wYWlyCiNkZWZpbmUgcGIgcHVzaF9iYWNrCiNkZWZpbmUgcGYgcHVzaF9mcm9udAojZGVmaW5lIHBvYiBwb3BfYmFjawojZGVmaW5lIHBvZiBwb3BfZnJvbnQKI2RlZmluZSBzeiBzaXplKCkK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