#3792
Medium Algorithms Sum of increasing product blocks
Math Simulation
69.0% acceptance
Mar 31, 2026
4
1
You are given an integer n.
A sequence is formed as follows:
The 1st block contains 1.
The 2nd block contains 2 * 3.
The ith block is the product of the next i consecutive integers.
Let F(n) be the sum of the first n blocks.
Return an integer denoting F(n) modulo 109 + 7.
Solution
Rust
Time O(n²)
Space O(1)
impl Solution {
pub fn sum_of_blocks(n: i32) -> i32 {
const MOD: i64 = 1_000_000_007;
let mut sum = 0i64;
let mut cur = 1i64;
for i in 1..=n as i64 {
let mut prod = 1i64;
for _ in 0..i {
prod = prod * cur % MOD;
cur += 1;
}
sum = (sum + prod) % MOD;
}
sum as i32
}
}