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