title: Program to find sum in an infinite array within given ranges
body: <p><strong>Problem Statement: Given on Hackrearth</strong></p>
  <p>Given an array, <code>A</code> of N integers, and you have also defined the new array <code>B</code> as a concatenation of array <code>A</code> for an infinite number of times.
  For example, if the given array <code>A</code> is <code>[1,2,3]</code> then, infinite array <code>B</code> is <code>[1,2,3,1,2,3,1,2,3,.......]</code>.</p>
  <p>Now you are given Q queries, each query consists of two integers <code>L</code> and <code>R</code>. Your task is to find the sum of the subarray from index <code>L</code> to <code>R</code> (both inclusive) in the infinite array <code>B</code> for each query.</p>
  <p>Note: As the value of this output is very large, print the answer as modulus 10^9+7;</p>
  <p><strong>Solution:</strong></p>
  <pre><code>vector&lt;int&gt; sumInRanges(vector&lt;int&gt; &amp;arr, int n, vector&lt;vector&lt;long long&gt;&gt; &amp;qrr, int q) {
     
      int mod=1e9+7;
  
      vector&lt;long long&gt;sarr(n+1,0);    // to store the prefix sum
      
      for(int i=1;i&lt;=n;i++)
      {
          sarr[i]=(sarr[i-1]+arr[i-1])%mod;
      }
      
      vector&lt;int&gt;ans;               //to store the final answer         
      
      for(int i=0;i&lt;q;i++)
      {
          long long l,r;
          l=qrr[i][0];
          r=qrr[i][1];
          
          long long grp=(r/n)%mod;
          long long res=(grp*sarr[n])%mod;
          res=(res+sarr[r%n])%mod;
          long long rsum=res;
          
          l-=1;
          grp=(l/n)%mod;
          res=(grp*sarr[n])%mod;
          res=(res+sarr[l%n])%mod;
          long long lsum=res;
  
          int sum=((rsum-lsum+mod)%mod);
          ------------------------------
          ans.push_back(sum);
      }
  
      return ans;
  }
  </code></pre>
  <p><strong>My doubt:</strong>
  I have solved the above problem like this, what I did is I first found the prefix array sum and then find the <code>rsum</code> up to range R corresponding to infinite array B and <code>lsum</code> up to range L-1 corresponding to infinite array B and then find the final answer as <code>(rsum - lsum)%mod</code> but I got some test cases as WA but when I did like this <code>(rsum - lsum+mod)%mod</code> all tests cases passed, my doubt is that what is the significance of adding &quot;mod&quot; in the above-highlighted line in the solution.</p>