this question from hackerrank(https://www.hackerrank.com/challenges/sam-and-substrings)… requires us to print the sum of all possible substrings, (input is a string of digits), (123 => 1, 2, 3, 12, 13, 123 => 164) so first i tried brute force method (later i was able to come up with an O(N) algo wich pruned unneccassary operations) to identify all n^2 pairs i, j, and then found sum[i, j] (sum of substr starting at i, ending at j), since ans could be large, we had to print ans%10^9,

so utilising the fact that (a + b) % x = a % x + b % x

i did :

```
long sum=0;
for(i = 0, i < s.size(), i++)
{
dp[i] = arr[i];
sum = sum % 1000000007;
sum += dp[i];
for(j = i+1, j < s.size(), j++)
{
dp[j] = ((dp[j-1]% 1000000007)*10 + arr[j] ) % (1000000007);
sum += dp[j];
}
}
// cout sum;
```

the code works fine for sample tests but fails, when i submit it…

any suggestions where i am going wrong…

full code :

http://ideone.com/0NXG72