#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
const long long MOD = 998244353;
inline int expmod(long long b, long long e) {
  long long r = 1;
  while (e)
    if (e & 1)
      r = r * b % MOD, e ^= 1;
    else
      b = b * b % MOD, e >>= 1;
  return r;
}
int fac[N], ifac[N], cmb[N];
long long n;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  fac[0] = ifac[0] = 1;
  for (int i = 1, qwerty = N; i < qwerty; i++)
    fac[i] = i * 1LL * fac[i - 1] % MOD;
  for (int i = 1, qwerty = N; i < qwerty; i++)
    ifac[i] = expmod(fac[i], MOD - 2LL);
  for (int i = 1, qwerty = N; i < qwerty; i++)
    cmb[i] = fac[n] * 1LL * ifac[i] % MOD * ifac[n - i] % MOD;
  long long ans = 0;
  for (int i = 1, qwerty = n + 1; i < qwerty; i++) {
    int tmp1 = expmod(expmod(3, n - i) - 1, n);
    int tmp2 = expmod(3, (n - 1LL * i) * n);
    ans += ((i & 1 ? 1LL : MOD - 1LL) * 1LL * (tmp1 - tmp2 + MOD) % MOD) *
           (3LL * cmb[i] % MOD) % MOD;
    if (ans >= MOD) ans -= MOD;
  }
  for (int i = 1, qwerty = n + 1; i < qwerty; i++) {
    int tmp1 = expmod(3, n * (n - 1LL * i) + 1LL * i);
    ans += (2LL * (i & 1 ? 1LL : MOD - 1LL) % MOD) *
           ((1LL * tmp1) * cmb[i] % MOD) % MOD;
    if (ans >= MOD) ans -= MOD;
  }
  cout << ans << "\n";
}
