description: Denote a cyclic sequence of size n as an array s such that s_n is adjacent to s_1. The segment s[r, l] where l < r is the concatenation of s[r, n] and s[1, l].
  
  You are given an array a consisting of n integers. Define b as the cyclic sequence obtained from concatenating m copies of a. Note that b has size n ⋅ m.
  
  You are given an integer k where k = 1 or k is a prime number. Find the number of different segments in b where the sum of elements in the segment is divisible by k.
  
  Two segments are considered different if the set of indices of the segments are different. For example, when n = 3 and m = 2, the set of indices for segment s[2, 5] is \{2, 3, 4, 5\}, and for segment s[5, 2] is \{5, 6, 1, 2\}. In particular, the segments s[1, 6], s[2,1], …, s[6, 5] are considered as the same segment.
  
  Output the answer modulo 10^9 + 7.
  
  Input
  
  The first line contains three integers n, m, and k (1 ≤ n, m, k ≤ 2 ⋅ 10^5, k = 1 or k is a prime number).
  
  The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 2 ⋅ 10^5).
  
  Output
  
  Output an integer denoting the number of different segments in b where the sum of elements in the segment is divisible by k, modulo 10^9 + 7.
  
  Examples
  
  Input
  
  
  5 1 5
  1 2 3 4 3
  
  
  Output
  
  
  4
  
  
  Input
  
  
  5 1 5
  1 2 3 4 5
  
  
  Output
  
  
  5
  
  
  Input
  
  
  5 4 5
  1 2 3 4 5
  
  
  Output
  
  
  125
  
  Note
  
  In the first example, all valid segments are [1,4], [2, 3], [3, 5], and [4, 2].
  
  In the second example, one of the valid segments is [1, 5].
solution: #include <bits/stdc++.h>
  using namespace std;
  void fft(vector<complex<double> >& a, bool inv) {
    int n = a.size(), j = 0;
    vector<complex<double> > roots(n / 2);
    for (int i = 1; i < n; i++) {
      int bit = (n >> 1);
      while (j >= bit) {
        j -= bit;
        bit >>= 1;
      }
      j += bit;
      if (i < j) swap(a[i], a[j]);
    }
    double ang = 2 * acos(-1) / n * (inv ? -1 : 1);
    for (int i = 0; i < n / 2; i++) {
      roots[i] = complex<double>(cos(ang * i), sin(ang * i));
    }
    for (int i = 2; i <= n; i <<= 1) {
      int step = n / i;
      for (int j = 0; j < n; j += i) {
        for (int k = 0; k < i / 2; k++) {
          complex<double> u = a[j + k], v = a[j + k + i / 2] * roots[step * k];
          a[j + k] = u + v;
          a[j + k + i / 2] = u - v;
        }
      }
    }
    if (inv)
      for (int i = 0; i < n; i++) a[i] /= n;
  }
  vector<long long> multiply(vector<long long>& v, vector<long long>& w,
                             long long MOD) {
    int n = 2;
    while (n < v.size() + w.size()) n <<= 1;
    vector<complex<double> > v1(n), v2(n), r1(n), r2(n);
    for (int i = 0; i < v.size(); i++)
      v1[i] = complex<double>(v[i] >> 15, v[i] & 32767);
    for (int i = 0; i < w.size(); i++)
      v2[i] = complex<double>(w[i] >> 15, w[i] & 32767);
    fft(v1, 0);
    fft(v2, 0);
    for (int i = 0; i < n; i++) {
      int j = (i ? (n - i) : i);
      complex<double> ans1 = (v1[i] + conj(v1[j])) * complex<double>(0.5, 0);
      complex<double> ans2 = (v1[i] - conj(v1[j])) * complex<double>(0, -0.5);
      complex<double> ans3 = (v2[i] + conj(v2[j])) * complex<double>(0.5, 0);
      complex<double> ans4 = (v2[i] - conj(v2[j])) * complex<double>(0, -0.5);
      r1[i] = (ans1 * ans3) + (ans1 * ans4) * complex<double>(0, 1);
      r2[i] = (ans2 * ans3) + (ans2 * ans4) * complex<double>(0, 1);
    }
    fft(r1, 1);
    fft(r2, 1);
    vector<long long> ret(n);
    for (int i = 0; i < n; i++) {
      long long av = (long long)round(r1[i].real());
      long long bv =
          (long long)round(r1[i].imag()) + (long long)round(r2[i].real());
      long long cv = (long long)round(r2[i].imag());
      av %= MOD;
      bv %= MOD;
      cv %= MOD;
      ret[i] = (av << 30) + (bv << 15) + cv;
      ret[i] %= MOD;
      ret[i] += MOD;
      ret[i] %= MOD;
    }
    return ret;
  }
  const int MOD = 1e9 + 7;
  vector<int> P[200001];
  int SS[200002], PS[200002];
  int mul(int a, int b) { return 1LL * a * b % MOD; }
  int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int N, M, K, S = 0, ans = 0;
    vector<long long> A, B;
    cin >> N >> M >> K;
    A.resize(K);
    B.resize(K);
    for (int i = 1; i <= N; i++) {
      cin >> PS[i];
      SS[i] = PS[i] % K;
      PS[i] += PS[i - 1];
      PS[i] %= K;
      A[PS[i]]++;
      P[PS[i]].push_back(i);
    }
    for (int i = N - 1; i >= 0; i--) {
      SS[i] += SS[i + 1];
      SS[i] %= K;
    }
    for (int i = 0; i < M - 1; i++) {
      B[S]++;
      S += SS[0];
      S %= K;
    }
    A = multiply(A, B, MOD);
    for (int i = K; i < A.size(); i++) {
      A[i - K] += A[i];
      if (A[i - K] >= MOD) A[i - K] -= MOD;
    }
    if ((S + SS[0]) % K == 0) ans = MOD - M + 1;
    for (int i = 1; i <= N; i++) {
      int v = (2 * K - SS[i] - S) % K;
      ans = (ans + 1LL * M * A[(K - SS[i]) % K]) % MOD;
      ans = (ans +
             1LL * M *
                 (lower_bound(P[v].begin(), P[v].end(), i - 1) - P[v].begin())) %
            MOD;
      ans =
          (ans + 1LL * M *
                     (P[PS[i - 1]].end() -
                      lower_bound(P[PS[i - 1]].begin(), P[PS[i - 1]].end(), i))) %
          MOD;
    }
    cout << ans << '\n';
    return 0;
  }
  