Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python

from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(1000000)

INF = 10 ** 18
MOD = 10 ** 9 + 7

N = input()
A = map(int, raw_input().split())

m_fact = {}
def fact(n):
    if n in m_fact:
        return m_fact[n]
    if n == 1:
        m_fact[n] = 1
        return 1
    m_fact[n] = fact(n - 1) * n
    return m_fact[n]

m_rev = {}
def rev_mod(n):
    if n in m_rev:
        return m_rev[n]
    p = MOD - 2
    ret = 1
    p_cur = n
    while p > 0:
        if p % 2:
            ret *= p_cur
            ret %= MOD
        p_cur *= p_cur
        p_cur %= MOD
        p /= 2
    m_rev[n] = ret
    return ret

S = [0] * (N + 2)
S2 = [0] * (N + 2)

for i in range(1, N):
    num = (fact(N) * rev_mod(i + 1)) % MOD
    S[0] += num
    S[0] %= MOD
    S[i] -= num
    S[i] %= MOD
    S[N] -= num
    S[N] %= MOD
    S[N - i] += num
    S[N - i] %= MOD

S[0] += fact(N)
S[0] %= MOD
S[N] -= fact(N)
S[N] %= MOD

for i in range(N):
    S[i + 1] += S[i]
    S[i + 1] %= MOD

for i in range(1, N - 1):
    num = (fact(N) * rev_mod(i + 1) * rev_mod(i + 2) * 2) % MOD
    wid = min(i, N - 1 - i)
    S2[1] += num
    S2[1] %= MOD
    S2[wid + 1] -= num
    S2[wid + 1] %= MOD
    S2[N - wid] -= num
    S2[N - wid] %= MOD

for i in range(N):
    S2[i + 1] += S2[i]
    S2[i + 1] %= MOD

for i in range(N):
    S2[i + 1] += S2[i]
    S2[i + 1] %= MOD

ans = 0
for i in range(N):
    ans += S[i] * A[i]
    ans += S2[i] * A[i]
    ans %= MOD

print ans

Fixed submission:
#!/usr/bin/env python

from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(1000000)

INF = 10 ** 18
MOD = 10 ** 9 + 7

N = input()
A = map(int, raw_input().split())

m_fact = {}
def fact(n):
    if n in m_fact:
        return m_fact[n]
    if n == 1:
        m_fact[n] = 1
        return 1
    m_fact[n] = (fact(n - 1) * n) % MOD
    return m_fact[n]

m_rev = {}
def rev_mod(n):
    if n in m_rev:
        return m_rev[n]
    p = MOD - 2
    ret = 1
    p_cur = n
    while p > 0:
        if p % 2:
            ret *= p_cur
            ret %= MOD
        p_cur *= p_cur
        p_cur %= MOD
        p /= 2
    m_rev[n] = ret
    return ret

S = [0] * (N + 2)
S2 = [0] * (N + 2)

for i in range(1, N):
    num = (fact(N) * rev_mod(i + 1)) % MOD
    S[0] += num
    S[0] %= MOD
    S[i] -= num
    S[i] %= MOD
    S[N] -= num
    S[N] %= MOD
    S[N - i] += num
    S[N - i] %= MOD

S[0] += fact(N)
S[0] %= MOD
S[N] -= fact(N)
S[N] %= MOD

for i in range(N):
    S[i + 1] += S[i]
    S[i + 1] %= MOD

for i in range(1, N - 1):
    num = (fact(N) * rev_mod(i + 1) * rev_mod(i + 2) * 2) % MOD
    wid = min(i, N - 1 - i)
    S2[1] += num
    S2[1] %= MOD
    S2[wid + 1] -= num
    S2[wid + 1] %= MOD
    S2[N - wid] -= num
    S2[N - wid] %= MOD

for i in range(N):
    S2[i + 1] += S2[i]
    S2[i + 1] %= MOD

for i in range(N):
    S2[i + 1] += S2[i]
    S2[i + 1] %= MOD

ans = 0
for i in range(N):
    ans += S[i] * A[i]
    ans += S2[i] * A[i]
    ans %= MOD

print ans

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python

from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(1000000)

INF = 10 ** 18
MOD = 10 ** 9 + 7

N = input()
A = map(int, raw_input().split())

m_fact = {}
def fact(n):
    if n in m_fact:
        return m_fact[n]
    if n == 1:
        m_fact[n] = 1
        return 1
    m_fact[n] = fact(n - 1) * n
    return m_fact[n]

m_rev = {}
def rev_mod(n):
    if n in m_rev:
        return m_rev[n]
    p = MOD - 2
    ret = 1
    p_cur = n
    while p > 0:
        if p % 2:
            ret *= p_cur
            ret %= MOD
        p_cur *= p_cur
        p_cur %= MOD
        p /= 2
    m_rev[n] = ret
    return ret

S = [0] * (N + 2)
S2 = [0] * (N + 2)

for i in range(1, N):
    num = (fact(N) * rev_mod(i + 1)) % MOD
    S[0] += num
    S[0] %= MOD
    S[i] -= num
    S[i] %= MOD
    S[N] -= num
    S[N] %= MOD
    S[N - i] += num
    S[N - i] %= MOD

S[0] += fact(N)
S[0] %= MOD
S[N] -= fact(N)
S[N] %= MOD

for i in range(N):
    S[i + 1] += S[i]
    S[i + 1] %= MOD

for i in range(1, N - 1):
    num = (fact(N) * rev_mod(i + 1) * rev_mod(i + 2) * 2) % MOD
    wid = min(i, N - 1 - i)
    S2[1] += num
    S2[1] %= MOD
    S2[wid + 1] -= num
    S2[wid + 1] %= MOD
    S2[N - wid] -= num
    S2[N - wid] %= MOD

for i in range(N):
    S2[i + 1] += S2[i]
    S2[i + 1] %= MOD

for i in range(N):
    S2[i + 1] += S2[i]
    S2[i + 1] %= MOD

for i in range(N):
    S[i] *= A[i]
    S[i] %= MOD
    S2[i] *= A[i]
    S2[i] %= MOD

print (sum(S[:N]) + sum(S2[:N])) % MOD

Fixed submission:
#!/usr/bin/env python

from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(1000000)

INF = 10 ** 18
MOD = 10 ** 9 + 7

N = input()
A = map(int, raw_input().split())

m_fact = {}
def fact(n):
    if n in m_fact:
        return m_fact[n]
    if n == 1:
        m_fact[n] = 1
        return 1
    m_fact[n] = (fact(n - 1) * n) % MOD
    return m_fact[n]

m_rev = {}
def rev_mod(n):
    if n in m_rev:
        return m_rev[n]
    p = MOD - 2
    ret = 1
    p_cur = n
    while p > 0:
        if p % 2:
            ret *= p_cur
            ret %= MOD
        p_cur *= p_cur
        p_cur %= MOD
        p /= 2
    m_rev[n] = ret
    return ret

S = [0] * (N + 2)
S2 = [0] * (N + 2)

for i in range(1, N):
    num = (fact(N) * rev_mod(i + 1)) % MOD
    S[0] += num
    S[0] %= MOD
    S[i] -= num
    S[i] %= MOD
    S[N] -= num
    S[N] %= MOD
    S[N - i] += num
    S[N - i] %= MOD

S[0] += fact(N)
S[0] %= MOD
S[N] -= fact(N)
S[N] %= MOD

for i in range(N):
    S[i + 1] += S[i]
    S[i + 1] %= MOD

for i in range(1, N - 1):
    num = (fact(N) * rev_mod(i + 1) * rev_mod(i + 2) * 2) % MOD
    wid = min(i, N - 1 - i)
    S2[1] += num
    S2[1] %= MOD
    S2[wid + 1] -= num
    S2[wid + 1] %= MOD
    S2[N - wid] -= num
    S2[N - wid] %= MOD

for i in range(N):
    S2[i + 1] += S2[i]
    S2[i + 1] %= MOD

for i in range(N):
    S2[i + 1] += S2[i]
    S2[i + 1] %= MOD

ans = 0
for i in range(N):
    ans += S[i] * A[i]
    ans += S2[i] * A[i]
    ans %= MOD

print ans

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python

from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(1000000)

INF = 10 ** 18
MOD = 10 ** 9 + 7

N = input()
A = map(int, raw_input().split())

m_fact = {}
def fact(n):
    if n in m_fact:
        return m_fact[n]
    if n == 1:
        m_fact[n] = 1
        return 1
    m_fact[n] = fact(n - 1) * n
    return m_fact[n]

def rev_mod(n):
    p = MOD - 2
    ret = 1
    p_cur = n
    while p > 0:
        if p % 2:
            ret *= p_cur
            ret %= MOD
        p_cur *= p_cur
        p_cur %= MOD
        p /= 2
    return ret

S = [0] * (N + 2)
S2 = [0] * (N + 2)

for i in range(1, N):
    num = (fact(N) * rev_mod(i + 1)) % MOD
    S[0] += num
    S[0] %= MOD
    S[i] -= num
    S[i] %= MOD
    S[N] -= num
    S[N] %= MOD
    S[N - i] += num
    S[N - i] %= MOD

S[0] += fact(N)
S[0] %= MOD
S[N] -= fact(N)
S[N] %= MOD

for i in range(N):
    S[i + 1] += S[i]
    S[i + 1] %= MOD

for i in range(1, N - 1):
    num = (fact(N) * rev_mod(i + 1) * rev_mod(i + 2) * 2) % MOD
    wid = min(i, N - 1 - i)
    S2[1] += num
    S2[1] %= MOD
    S2[wid + 1] -= num
    S2[wid + 1] %= MOD
    S2[N - wid] -= num
    S2[N - wid] %= MOD

for i in range(N):
    S2[i + 1] += S2[i]
    S2[i + 1] %= MOD

for i in range(N):
    S2[i + 1] += S2[i]
    S2[i + 1] %= MOD

for i in range(N):
    S[i] *= A[i]
    S[i] %= MOD
    S2[i] *= A[i]
    S2[i] %= MOD

print (sum(S[:N]) + sum(S2[:N])) % MOD

Fixed submission:
#!/usr/bin/env python

from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(1000000)

INF = 10 ** 18
MOD = 10 ** 9 + 7

N = input()
A = map(int, raw_input().split())

m_fact = {}
def fact(n):
    if n in m_fact:
        return m_fact[n]
    if n == 1:
        m_fact[n] = 1
        return 1
    m_fact[n] = (fact(n - 1) * n) % MOD
    return m_fact[n]

m_rev = {}
def rev_mod(n):
    if n in m_rev:
        return m_rev[n]
    p = MOD - 2
    ret = 1
    p_cur = n
    while p > 0:
        if p % 2:
            ret *= p_cur
            ret %= MOD
        p_cur *= p_cur
        p_cur %= MOD
        p /= 2
    m_rev[n] = ret
    return ret

S = [0] * (N + 2)
S2 = [0] * (N + 2)

for i in range(1, N):
    num = (fact(N) * rev_mod(i + 1)) % MOD
    S[0] += num
    S[0] %= MOD
    S[i] -= num
    S[i] %= MOD
    S[N] -= num
    S[N] %= MOD
    S[N - i] += num
    S[N - i] %= MOD

S[0] += fact(N)
S[0] %= MOD
S[N] -= fact(N)
S[N] %= MOD

for i in range(N):
    S[i + 1] += S[i]
    S[i + 1] %= MOD

for i in range(1, N - 1):
    num = (fact(N) * rev_mod(i + 1) * rev_mod(i + 2) * 2) % MOD
    wid = min(i, N - 1 - i)
    S2[1] += num
    S2[1] %= MOD
    S2[wid + 1] -= num
    S2[wid + 1] %= MOD
    S2[N - wid] -= num
    S2[N - wid] %= MOD

for i in range(N):
    S2[i + 1] += S2[i]
    S2[i + 1] %= MOD

for i in range(N):
    S2[i + 1] += S2[i]
    S2[i + 1] %= MOD

ans = 0
for i in range(N):
    ans += S[i] * A[i]
    ans += S2[i] * A[i]
    ans %= MOD

print ans