#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define int long long
namespace
{
int N, M;
vector<int> A;
vector<int> prefixSum;
vector<int> moddedPrefixBIT;
int ans;
void inputInfo();
void findAns();
void addToBit(int x, int value);
int query(int x);
void outputAns();
}
int32_t main()
{
while (cin >> N >> M)
{
inputInfo();
findAns();
outputAns();
}
}
namespace
{
void inputInfo()
{
A.clear(), A.resize(N + 1);
for (int i = 1; i <= N; i++)
{
cin >> A[i];
}
}
void findAns()
{
// Mod all values in initial array
for (int i = 1; i <= N; i++)
{
A[i] %= M;
}
// Prefix sum
prefixSum.clear(), prefixSum.resize(N + 1);
prefixSum[0] = 0;
for (int i = 1; i <= N; i++)
{
prefixSum[i] = prefixSum[i - 1] + A[i];
}
// Calculate total sum
int totalSum = 0;
for (int i = 1; i <= N; i++)
{
totalSum += prefixSum[i] % M;
}
// Create Fenwick tree
moddedPrefixBIT.clear(), moddedPrefixBIT.resize(M + 1);
for (int i = 1; i <= N; i++)
{
addToBit(prefixSum[i] % M + 1, 1);
}
// Calculate answer
ans = 0;
int reducedSum = 0;
int termsCount = N;
for (int i = 1; i <= N; i++)
{
// Count # of modded prefix sum < reducedSum
int negativeReducedCount = query(reducedSum);
// Update answer
ans += totalSum;
ans -= reducedSum * termsCount;
ans += negativeReducedCount * M;
// Update state
totalSum -= prefixSum[i] % M;
reducedSum += A[i];
reducedSum %= M;
termsCount--;
addToBit(prefixSum[i] % M + 1, -1);
}
}
void addToBit(int x, int value)
{
for (int i = x; i <= M; i += (i & (-i)))
{
moddedPrefixBIT[i] += value;
}
}
int query(int x)
{
int value = 0;
for (int i = x; i > 0; i -= (i & (-i)))
{
value += moddedPrefixBIT[i];
}
return value;
}
void outputAns()
{
cout << ans << '\n';
}
}